mysqli_insert_id()函數(shù)返回最后一條插入語句產(chǎn)生的自增 ID
如果您有一個具有列的AUTO_INCREMENT屬性的表,并且如果您的最后一次MySQLi函數(shù)調(diào)用執(zhí)行INSERT或UPDATE語句。 函數(shù)的作用是:返回自動生成的上次執(zhí)行查詢的ID。
mysqli_insert_id($con)
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
mysqli_insert_id() 函數(shù)返回最后一個 SQL 語句(通常是 INSERT 語句) 所操作的表中設(shè)置為 AUTO_INCREMENT 的列的值。 如果最后一個 SQL 語句不是 INSERT 或者 UPDATE 語句, 或者所操作的表中沒有設(shè)置為 AUTO_INCREMENT 的列, 返回值為 0。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
假設(shè)我們在數(shù)據(jù)庫mydb中創(chuàng)建了一個名為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()函數(shù)的用法(面向過程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //在employee表中插入記錄 $sql = "insert into Cricketers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India')"; mysqli_query($con, $sql); //插入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); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
Insert ID: 1 Insert ID: 2
在面向?qū)ο蟮臉邮街?,此函?shù)的語法為$ con-> insert_id();。以下是面向?qū)ο髽邮? minus的This函數(shù)的示例;
<?php //建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); //在employee表中插入記錄 $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"); //在employee表中插入記錄 $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); //關(guān)閉連接 $con -> close(); ?>
輸出結(jié)果
Insert ID: 3 Insert ID: 4
以下是函數(shù)mysqli_insert_id的另一個示例-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //選擇Cricketers表的所有行的查詢 mysqli_query($con, "SELECT * FROM Cricketers"); print("Insert ID (select query): ".mysqli_insert_id($con)."\n"); //用于將多行插入到Cricketers表中的查詢 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"); //查詢以更新employee表的行 mysqli_query($con, "UPDATE Cricketers set COUNTRY = 'S.Africa' where ID = 2"); print("Insert ID (update query): ".mysqli_insert_id($con)."\n"); //查詢將記錄插入到employee表中 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"); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
Insert ID (select query): 0 Insert ID: (multiple inserts) 6 Insert ID (update query): 0 Insert ID: (table with out auto incremented key) 0
以下示例演示了SELECT,UPDATE,INSERT和DELETE查詢中mysqli_insert_id函數(shù)的用法-
<?php $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb"); if (mysqli_connect_errno($connection_mysql)){ echo "連接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); ?>
輸出結(jié)果
New record has id: 0