PHP 教程

PHP 教程 PHP 简介 PHP 下载安装 PHP 语法 PHP 注释 PHP 变量 PHP Echo / Print PHP 数据类型 PHP 字符串 PHP 数值 PHP Math PHP 常量 PHP 运算符 PHP If...Else...Elseif PHP Switch PHP 循环 PHP 函数 PHP 数组 PHP 全局变量 PHP 正则表达式

PHP 表单

PHP 表单处理 PHP 表单验证 PHP 表单验证必填字段 PHP 表单验证URL/E-mail PHP 表单验证实例

PHP 高级教程

PHP 日期和时间 PHP Include PHP 文件处理 PHP 文件打开/读取 PHP 文件创建/写入 PHP 文件上传 PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP 回调函数 PHP JSON PHP 异常处理

PHP 面向对象

PHP 什么是 OOP PHP 类和对象 PHP 构造函数 PHP 析构函数 PHP 访问修饰符 PHP 继承 PHP 类常量 PHP 抽象类 PHP 接口 PHP 特征 PHP 静态方法 PHP 静态属性 PHP 命名空间 PHP 可迭代对象

MySQL 数据库

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP 实例

PHP 实例 PHP 编译器 PHP 测验 PHP 练习

PHP 参考手册

PHP 概述 PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones


PHP MySQL 获取上次插入记录的 ID

获取上次插入记录的 ID

如果我们对具有自动增量字段的表执行插入或更新,我们可以立即获得上次插入/更新记录的ID。

在表 "MyGuests" 中,"id" 列是一个自动递增字段:

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 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

以下示例与上一页中的示例相同,只是我们添加了一行代码来检索最后插入的记录的 ID。我们还回显最后插入的 ID:

实例 (MySQLi - 面向对象)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
  $last_id = $conn->insert_id;
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>



实例 (MySQLi - 面向过程)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if (mysqli_query($conn, $sql)) {
  $last_id = mysqli_insert_id($conn);
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

实例 (PDO)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('John', 'Doe', 'john@example.com')";
  // use exec() because no results are returned
  $conn->exec($sql);
  $last_id = $conn->lastInsertId();
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} catch(PDOException $e) {
  echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>