(PHP 5, PHP 7)
mysqli::$insert_id -- mysqli_insert_id — 锟斤拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� ID
锟斤拷锟斤拷锟斤拷锟斤拷锟�
锟斤拷锟教伙拷锟斤拷锟�
mysqli_insert_id() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟� SQL 锟斤拷洌ㄍ拷锟斤拷锟� INSERT 锟斤拷洌� 锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷为 AUTO_INCREMENT 锟斤拷锟叫碉拷值锟斤拷 锟斤拷锟斤拷锟斤拷一锟斤拷 SQL 锟斤拷洳伙拷锟� INSERT 锟斤拷锟斤拷 UPDATE 锟斤拷洌� 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷没锟斤拷锟斤拷锟斤拷为 AUTO_INCREMENT 锟斤拷锟叫o拷 锟斤拷锟斤拷值为 0锟斤拷
Note:
锟斤拷锟斤拷锟斤拷锟街达拷械锟� INSERT 锟斤拷锟斤拷 UPDATE 锟斤拷锟斤拷锟绞癸拷锟斤拷锟斤拷锟斤拷菘夂拷锟� LAST_INSERT_ID()锟斤拷 锟叫匡拷锟杰伙拷影锟斤拷 mysqli_insert_id() 锟斤拷锟斤拷锟侥凤拷锟斤拷值锟斤拷
锟斤拷锟揭伙拷锟� SQL锟斤拷INSERT 锟斤拷锟斤拷 UPDATE锟斤拷锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷为 AUTO_INCREMENT 锟斤拷锟皆碉拷锟叫碉拷值锟斤拷 锟斤拷锟街革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿粗达拷锟� SQL 锟斤拷洌拷锟斤拷锟斤拷锟斤拷一锟斤拷 SQL 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷谋锟斤拷锟矫伙拷锟斤拷锟轿� AUTO_INCREMENT 锟斤拷锟叫o拷锟斤拷锟斤拷 0锟斤拷
Note:
锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟� php 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷 mysqli_insert_id() 锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷锟街碉拷锟�
Example #1 $mysqli->insert_id 锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷锟�
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* 锟斤拷锟斤拷锟斤拷锟� */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
/* 删锟斤拷锟斤拷 */
$mysqli->query("DROP TABLE myCity");
/* 锟截憋拷锟斤拷锟斤拷 */
$mysqli->close();
?>
锟斤拷锟教伙拷锟斤拷锟�
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* 锟斤拷锟斤拷锟斤拷锟� */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($link, "CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($link, $query);
printf ("New Record has id %d.\n", mysqli_insert_id($link));
/* 删锟斤拷锟斤拷 */
mysqli_query($link, "DROP TABLE myCity");
/* 锟截憋拷锟斤拷锟斤拷 */
mysqli_close($link);
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
New Record has id 1.