mysql_insert_id() and potential bugs?

wxdqz

New Member
Environment:
Kernel: 2.2.16-22
Distribution: Red Hat 7.0
Mysql: Ver 11.16 Distrib 4.0.0-alpha, for pc-linux-gnu (i686)
php: 4.0.1pl2

Problem:
I use the following code in PHP:

<?php
...

function insertXXX($var1, $var2, $var3, ...)
{
$sql = generateInsertSQL($var1, $var2, $var3,...);
mysql_query($sql);
$index = mysql_index_id();
return $index;
}
...
?>

In MySQL I have a table setup (the table I am inserting into in the previous php code) that has an auto-increment PK, the SQL code that I generate from the generateInsertSQL($var1, $var2, $var3, ...) is of the form:
INSERT INTO TABLE_NAME (col2, col3, col4, ...) VALUES($var1, $var2, $var3, ...)

where col1 is the auto-increment PK field!!!!!! I get the correct last index ID from mysql_index_id() function, however when I call mysql_index_id() straight after my insert the insert is not committed to the database. When I remove the mysql_index_id() and hardcode the index (for debugging purposes only), the insert is committed, but of course I cannot get the last index ID inserted!!!!!

Am I doing something horribly wrong and am too close to the problem to see it? Or is this a bug in either PHP or MySQL? If this is a bug, what are the alternatives, since I noticed stored procedures are not an option until version 4.1 of MySQL? I would prefer not to have to do a very inefficient work around, for obvious reasons, so any REAL ALTERNATIVES to this method of getting the last index ID would be gratefully accepted.

Thanks,
Su
 
Back
Top