ID: 32195
User updated by: toma at smartsemantics dot com
Reported By: toma at smartsemantics dot com
Status: Open
Bug Type: MySQLi related
Operating System: Linux RedHat Enterprise 3
PHP Version: 5.0.3
New Comment:
I reversed my expected and actual results... what I am looking to get
is insert ids with a value of zero. Please reverse them.
Previous Comments:
------------------------------------------------------------------------
[2005-03-05 00:26:30] toma at smartsemantics dot com
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 this bug report at http://bugs.php.net/?id=32195&edit=1