The example is lack of insert_id in multi_query. Here is my example:
Assuming you have a new test_db in mysql like this:
create database if not exists test_db;
use test_db;
create table user_info (_id serial, name varchar(100) not null);
create table house_info (_id serial, address varchar(100) not null);
Then you run a php file like this:
<?php
define('SERVER', '127.0.01');
define('MYSQL_USER', 'your_user_name');
define('MYSQL_PASSWORD', 'your_password');
$db = new mysqli(SERVER, MYSQL_USER, MYSQL_PASSWORD, "test_db", 3306);
if ($db->connect_errno)
echo "create db failed, error is ", $db->connect_error;
else {
$sql = "insert into user_info "
. "(name) values "
. "('owen'), ('john'), ('lily')";
if (!$result = $db->query($sql))
echo "insert failed, error: ", $db->error;
else
echo "last insert id in query is ", $db->insert_id, "\n";
$sql = "insert into user_info"
. "(name) values "
. "('jim');";
$sql .= "insert into house_info "
. "(address) values "
. "('shenyang')";
if (!$db->multi_query($sql))
echo "insert failed in multi_query, error: ", $db->error;
else {
echo "last insert id in first multi_query is ", $db->insert_id, "\n";
if ($db->more_results() && $db->next_result())
echo "last insert id in second multi_query is ", $db->insert_id, "\n";
else
echo "insert failed in multi_query, second query error is ", $db->error;
}
$db->close();
}
?>
You will get output like this:
last insert id in query is 1
last insert id in first multi_query is 4
last insert id in second multi_query is 1
Conclusion:
1 insert_id works in multi_query
2 insert_id is the first id mysql has used if you have insert multi values