PHP mysqli_insert_id() 函数



定义和用法

如果您有一个表,其中一列具有 AUTO_INCREMENT 属性,并且如果您上次的 MySQLi 函数调用执行了 INSERT 或 UPDATE 语句。mysqli_insert_id() 函数返回上次执行的查询的自动生成的 ID。

语法

mysqli_insert_id($con)

参数

序号 参数及描述
1

con(必填)

这是一个表示与 MySQL 服务器连接的对象。

返回值

PHP mysqli_insert_id() 函数返回上次查询中“自动递增”列的值,如果是 INSERT 或 UPDATE 操作。如果上次执行的查询不是 INSERT 或 UPDATE,或者表中没有任何列/字段具有“AUTO_INCREMENT”属性,则此函数返回 0

PHP 版本

此函数首次引入 PHP 5 版本,并在所有后续版本中均有效。

假设我们在数据库 mydb 中创建了一个名为 Cricketers 的表,其中字段 ID 是 PRIMARY KEY 并且它是 AUTO INCREMENTED 如下所示:

CREATE TABLE Cricketers(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   First_Name VARCHAR(255), 
   Last_Name VARCHAR(255), 
   Date_Of_Birth date, 
   Place_Of_Birth VARCHAR(255), 
   Country VARCHAR(255)
);

示例

以下示例演示了 mysqli_insert_id() 函数(以过程式风格)的用法:

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Inserting a record into the employee table
   $sql = "insert into Cricketers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India')";
   mysqli_query($con, $sql);
   //Insert ID
   $id = mysqli_insert_id($con);
   print("Insert ID: ".$id ."\n");

   $sql = "insert into Cricketers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica')";
   mysqli_query($con, $sql);
   $id = mysqli_insert_id($con);
   print("Insert ID: ".$id);

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Insert ID: 1
Insert ID: 2

示例

在面向对象风格中,此函数的语法为 $con->insert_id(); 以下是此函数在面向对象风格中的示例:

<?php
   //Creating a connection
   $con = new mysqli("localhost", "root", "password", "mydb");

   //Inserting a record into the employee table
   $con -> query("insert into Cricketers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka')");
   //Insert ID
   $state = $con->insert_id;
   print("Insert ID: ".$state."\n");

   //Inserting a record into the employee table
   $con -> query("insert into Cricketers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India')");
   //Insert ID
   $state = $con->insert_id;
   print("Insert ID: ".$state);

   //Closing the connection
   $con -> close();
?>

这将产生以下结果:

Insert ID: 3
Insert ID: 4

示例

以下是函数 mysqli_insert_id 的另一个示例:

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Query to SELECT all the rows of the Cricketers table
   mysqli_query($con, "SELECT * FROM Cricketers");
   print("Insert ID (select query): ".mysqli_insert_id($con)."\n");

   //Query to INSERT multiple rows into the Cricketers table
   mysqli_query($con, "INSERT INTO Cricketers VALUES (5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'), (6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India') ");
   print("Insert ID: (multiple inserts) ".mysqli_insert_id($con)."\n");

   //Query to UPDATE the rows of the employee table
   mysqli_query($con, "UPDATE Cricketers set COUNTRY = 'S.Africa' where ID = 2");
   print("Insert ID (update query): ".mysqli_insert_id($con)."\n");

   //Query to INSERT a record into the employee table
   mysqli_query($con, "INSERT INTO employee VALUES ('Sarmista', 'Sharma', 28, 'F', 15000,  101)");
   print("Insert ID: (table with out auto incremented key) ".mysqli_insert_id($con)."\n");

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:

Insert ID (select query): 0
Insert ID: (multiple inserts) 6
Insert ID (update query): 0
Insert ID: (table with out auto incremented key) 0

示例

以下示例演示了 mysqli_insert_id 函数与 SELECT、UPDATE、INSERT 和 DELETE 查询一起使用的用法:

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   
   mysqli_query($connection_mysql,"INSERT INTO Employee (name) VALUES('PHP')");
   echo "New record has id: " . mysqli_insert_id($connection_mysql); 
   
   mysqli_close($connection_mysql);
?>

这将产生以下结果:

New record has id: 0
php_function_reference.htm
广告