定义和用法
mysql_insert_id() 函数返回上一步 INSERT *** 作产生的 ID。
如果上一查询没有产生 AUTO_INCREMENT 的 ID,则 mysql_insert_id() 返回 0。
语法
mysql_insert_id(connection)
例子
<?php
$con = mysql_connect("localhost", "hello", "321")
if (!$con)
{
die('Could not connect: ' . mysql_error())
}
$db_selected = mysql_select_db("test_db",$con)
$sql = "INSERT INTO person VALUES ('Carter','Thomas','Beijing')"
$result = mysql_query($sql,$con)
echo "ID of last inserted record is: " . mysql_insert_id()
mysql_close($con)
?>
$servername = "localhost"
$username = "username"
$password = "password"
$dbname = "myDB"
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname)
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error)
}
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)"
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully"
} else {
echo "Error creating table: " . $conn->error
}
$conn->close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)