views:

347

answers:

2

Hello,

I have a MySql table whose primary key is a 64bit BigInt

I'm using Zend_Db (Zend Framework 1.8.4) to insert a new row, then call lastInsertId() to retreive the new row's id, what I get back is a super large number such as 18446744072633694008, and this number changes from time to time, but always this large. the auto increment index is set to 0, and in the database the record actually got inserted with correct primary id, (0, 1, 2...), it's just that the id return from lastInsertId() gives weird number. Is it a known issue for Zend_db, which doesn't deal with 64 bit number?

environment: Zend Framework 1.8.4 Apache2 on 32bit box, Ubuntu OS MySQL5.1 PHP5.2.4 MySQL adapter: mysqli

Thanks

+1  A: 

I developed a lot of Zend_Db code through ZF 1.0.

PHP database extensions return PHP strings, not integers. The reason for this is that the RDBMS is likely to be using numeric data types that exceed the range of integers supported in the client PHP binary.

Zend_Db just returns the values it fetches from the PHP database extension, so if you see strange values, then it's probably due to PHP, not Zend_Db.

You might want to run the unit tests to test inserts:

$ cd <zf>/tests
$ vi TestConfiguration.php   # enable the MySQL adapter you're using
$ phpunit --verbose --filter testAdapterInsert Zend/Db/AllTests.php

One of the assertions made in these tests is that the return value from lastInsertId() is a PHP string type.

Bill Karwin
Thanks for the answer, I'll try the unit test.
Bill, that's because the adapter just casts it to a string. It is a bug in the PHP's OO implementation of the Mysqli adapter, that is apparently not present in the procedural style. I'm filing a ticket shortly...
jason
Yep, you're right, I see the code now.
Bill Karwin
+2  A: 

This seems to be a bug in the OO implementation of PHP's Mysqli adapter. See this note on PHP's website.

For a temporary, stop-gap solution, try using the PDO_Mysql Zend_Db adapter.

I'm currently creating an issue and working on a solution to see if I can work around this problem in Zend_Db_Adapter_Mysqli. I will keep this answer up to date with my progress.

You can follow my progress on a workaround at http://framework.zend.com/issues/browse/ZF-7590

jason
aha, now it makes sense. Please keep me updated on the fix. The table's primary key is getting very close to that magic number 4,294,967,295, so we have to upgrade int32 to int64. Thank you
Actually, I wonder if this issue would exist on a 64bit system? our production server is 64bit, but dev server is 32bit (I know, we should make them the same). maybe I'll do some testing
On true 64 bit platforms, PHP's native integer is indeed 64 bit, I believe.
jason