当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_insert_id()用法及代码示例



定义和用法

如果您有一个包含 AUTO_INCREMENT 属性的表,并且您的最后一个 MySQLi 函数调用执行了 INSERT 或 UPDATE 语句。这mysqli_insert_id()函数返回上次执行查询的自动生成的 id。

用法

mysqli_insert_id($con)

参数

Sr.No 参数及说明
1

con(Mandatory)

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

返回值

PHP mysqli_insert_id() 函数返回上次查询中 "Auto Increment" 列的值 如果是 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();以下是面向对象样式 $minus 中此函数的示例;

<?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 mysqli_insert_id() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。