|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-03-05 00:34 UTC] toma at smartsemantics dot com
[2005-03-06 16:14 UTC] sniper@php.net
[2005-03-07 10:21 UTC] georg@php.net
[2005-03-07 16:37 UTC] toma at smartsemantics dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 05:00:02 2025 UTC |
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='$p@rkpl@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 ) ) ) )