PHP mysqli_insert_id()Function
Assume the websites table has an auto-generated ID field. Returns the ID from the last query:
<?php
// 假定数据库用户名:root,密码:123456,数据库:EXAMPLE
$con=mysqli_connect("localhost","root","123456","EXAMPLE");
if (mysqli_connect_errno($con))
{
echo "连接 MySQL 失败: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO websites (name,url,alexa)
VALUES ('Example','http://www.example.com',5633)");
// 输出自动生成的 ID
echo "新 id 为: " . mysqli_insert_id($con);
mysqli_close($con);
?>
Definition and Usage
The mysqli_insert_id() function returns the ID auto-generated in the last query (generated by AUTO_INCREMENT).
Syntax
mysqli_insert_id(connection);
| Parameters | Description |
|---|---|
| connection | Required. Specifies the MySQL connection to use. |
Technical Details
| Return value: | Returns an integer with the AUTO_INCREMENT field value auto-generated in the last query. If the number > maximum integer value, it returns a string. If there is no update or no AUTO_INCREMENT field, it returns 0. |
|---|---|
| PHP Version: | 5+ |
PHP MySQLi Reference Manual Other Extensions