From: toma at smartsemantics dot com
Operating system: Linux RedHat Enterprise 3
PHP version: 5.0.3
PHP Bug Type: MySQLi related
Bug description: mysqli_insert_id with mysqli_multi_query returns wrong value
Description:
------------
I am executing a loop of 4 statements: 2 INSERTS returning insert IDs and
2 SELECTS. The last 2 SELECTS both show insert IDs equal to the value of
the last successful INSERT. I believe the appropriate behavior should be
that mysqli_insert_id return a value of zero or null for the SELECTs.
Reproduce code:
---------------
<?php
$server='localhost';
$user='parkdomains';
$pass='[EMAIL PROTECTED]@ce$';
$database='ParkDomains';
###CONNECT TO DATABASE
$dbconn=mysqli_init();
mysqli_options($dbconn,MYSQLI_OPT_CONNECT_TIMEOUT,8);
//mysqli_real_connect($dbconn,$server,$user,$pass,$database,null,null,MYSQLI_CLIENT_FOUND_ROWS);
mysqli_real_connect($dbconn,$server,$user,$pass,$database,null,null,MYSQLI_CLIENT_FOUND_ROWS);
if(mysqli_connect_errno()){
printf("Can't connect to the MySQL Server (".$server."): %s\n",
mysqli_connect_error());
}
@mysqli_select_db($dbconn,$database);
if(mysqli_error($dbconn)) $ReportError(sprintf("Can't select the MYSQL
database (".$database."): %s\n", mysqli_error($dbconn)),1);
###OUR SQL STATEMENTS###
$sql_statements_statements=array();
$sql_statements[]="INSERT INTO test () VALUES('','test')";
$sql_statements[]="INSERT INTO test () VALUES('','test')";
$sql_statements[]="SELECT * FROM test LIMIT 3";
$sql_statements[]="SELECT * FROM test LIMIT 2";
###COMBINE THEM INTO ONE###
$query=implode(';',$sql_statements);
echo $query."<BR>\n";
$ret=array();
reset($sql_statements);
if(mysqli_multi_query($dbconn,$query)){
do{
$obj=new stdClass();
list(,$obj->sql)=each($sql_statements);
$res=mysqli_store_result($dbconn);
$obj->affected_rows=mysqli_affected_rows($dbconn);
$obj->last_insert_id=mysqli_insert_id($dbconn);
if($res){
if(mysqli_num_rows($res)>0){
while ($row=mysqli_fetch_row($res)){
$obj->result[]=$row;
};
mysqli_free_result($res);
}
}
$ret[]=$obj;
}while(mysqli_next_result($dbconn));
}
echo "<pre>";
print_r($ret);
?>
Expected result:
----------------
INSERT INTO test () VALUES('','test');INSERT INTO test ()
VALUES('','test');SELECT * FROM test LIMIT 3;SELECT * FROM test LIMIT 2
Array
(
[0] => stdClass Object
(
[sql] => INSERT INTO test () VALUES('','test')
[affected_rows] => 1
[last_insert_id] => 46
)
[1] => stdClass Object
(
[sql] => INSERT INTO test () VALUES('','test')
[affected_rows] => 1
[last_insert_id] => 47
)
[2] => stdClass Object
(
[sql] => SELECT * FROM test LIMIT 3
[affected_rows] => 3
[last_insert_id] => 47
[result] => Array
(
[0] => Array
(
[0] => 30
[1] => 0
)
[1] => Array
(
[0] => 31
[1] => 0
)
[2] => Array
(
[0] => 32
[1] => 0
)
)
)
[3] => stdClass Object
(
[sql] => SELECT * FROM test LIMIT 2
[affected_rows] => 2
[last_insert_id] => 47
[result] => Array
(
[0] => Array
(
[0] => 30
[1] => 0
)
[1] => Array
(
[0] => 31
[1] => 0
)
)
)
)
Actual result:
--------------
INSERT INTO test () VALUES('','test');INSERT INTO test ()
VALUES('','test');SELECT * FROM test LIMIT 3;SELECT * FROM test LIMIT 2
Array
(
[0] => stdClass Object
(
[sql] => INSERT INTO test () VALUES('','test')
[affected_rows] => 1
[last_insert_id] => 46
)
[1] => stdClass Object
(
[sql] => INSERT INTO test () VALUES('','test')
[affected_rows] => 1
[last_insert_id] => 47
)
[2] => stdClass Object
(
[sql] => SELECT * FROM test LIMIT 3
[affected_rows] => 3
[last_insert_id] => 0
[result] => Array
(
[0] => Array
(
[0] => 30
[1] => 0
)
[1] => Array
(
[0] => 31
[1] => 0
)
[2] => Array
(
[0] => 32
[1] => 0
)
)
)
[3] => stdClass Object
(
[sql] => SELECT * FROM test LIMIT 2
[affected_rows] => 2
[last_insert_id] => 0
[result] => Array
(
[0] => Array
(
[0] => 30
[1] => 0
)
[1] => Array
(
[0] => 31
[1] => 0
)
)
)
)
--
Edit bug report at http://bugs.php.net/?id=32195&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32195&r=trysnapshot4
Try a CVS snapshot (php5.0):
http://bugs.php.net/fix.php?id=32195&r=trysnapshot50
Try a CVS snapshot (php5.1):
http://bugs.php.net/fix.php?id=32195&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32195&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32195&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32195&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32195&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32195&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32195&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32195&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=32195&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=32195&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32195&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32195&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32195&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32195&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32195&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32195&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32195&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32195&r=mysqlcfg