简体   繁体   中英

Behavior of mysql_insert_id()

I am using functions from a php library that uses mysql_insert_id() every now and then. Now I've modified a few functions that insert data into tables such that the primary key value is explicitly specified eg I insert an a row with ID=300 followed by a row with ID=200 when the table originally had 10 rows. I am wondering if mysql_insert_id will behave as expected ie return the value I explicitly specified for the AUTO_INCREMENT PRIMARY KEY column.

Table:

CREATE TABLE `foo` (
  `id` int(11) NOT NULL auto_increment,
  `value` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ;

Code:

print phpversion()."\n";
mysql_connect('localhost','user','pass');
mysql_select_db('database');

mysql_query('INSERT INTO `foo` (`id`,`value`) VALUES(100,"bar")');
print var_export(mysql_insert_id())."\n";

mysql_query('INSERT INTO `foo` (`value`) VALUES("zoid")');
print var_export(mysql_insert_id())."\n";

mysql_query('INSERT INTO `foo` (`id`,`value`) VALUES(99,"yargh")');
print var_export(mysql_insert_id())."\n";

$q = mysql_query('SHOW TABLE STATUS LIKE "foo"');
$status = mysql_fetch_assoc($q);
mysql_free_result($q);
print $status['Auto_increment']."\n";

mysql_query('INSERT INTO `foo` (`value`) VALUES("whatever")');
print var_export(mysql_insert_id())."\n";

Output:

5.2.XX-0.dotdeb.1
100
101
99
102
102

TL/DR version: it returns the last id, be it auto incremented, or explicitly given.

UPDATE: Added INSERT with id < 100 , as suggested by Mchl.

The answer is yes,

it will return the values you entered. i liked this question so i actually tried multiple times in multiple servers having diffrent configurations.

it will return the value you inserted,

the mysql_insert_id() returns the value of the auto_increment feild, it need not be auto generated.

Also it will return 0, if you try to give dupilcate value for your key because it wont get inserted properly.

if you want to try then use the following query

--EDIT .. with more inserts

<?php
$link = mysql_connect('localhost', 'root', 'techping');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('test');

mysql_query("INSERT INTO test (id, name) values (100,'kossu')");
printf("Last inserted record has id %d<br />", mysql_insert_id());
mysql_query("INSERT INTO test (id, name) values (125,'kossu')");
printf("Last inserted record has id %d<br />", mysql_insert_id());
mysql_query("INSERT INTO test (id, name) values (121,'kossu')");
printf("Last inserted record has id %d<br />", mysql_insert_id());
mysql_query("INSERT INTO test (id, name) values ('','kossu')");
printf("Last inserted record has id %d<br />", mysql_insert_id());
mysql_query("INSERT INTO test (id, name) values (250,'kossu')");
printf("Last inserted record has id %d<br />", mysql_insert_id());
?>

The results:

Last inserted record has id 100 
Last inserted record has id 125
Last inserted record has id 121
Last inserted record has id 126
Last inserted record has id 250

See when i let the autoincrement generate the id after 121, it generated the next value(126) after the biggest id( ie; 125 in this case ), not 122

I don't think it will. It should return 0:

The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established. - http://php.net/manual/en/function.mysql-insert-id.php

The ID isn't generated so should return 0.

As has been proven, in practice this is not the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM