Skip to content

MySQL 删除数据

使用 MySQLi 和 PDO 从数据库表中删除数据

Section titled “使用 MySQLi 和 PDO 从数据库表中删除数据”

SQL DELETE 语句用于从数据库表中移除一个或多个记录(行)。

基本语法如下:

DELETE FROM table_name
WHERE condition;
⚠️重要: WHERE 子句指定要删除哪些记录。如果省略 WHERE 子句,DELETE 语句将删除表中的所有记录!在执行 DELETE 查询之前,尤其是在生产环境中,务必仔细检查您的 WHERE 子句。

condition 通常涉及比较列的值(例如 id = 3, status = 'inactive')。

要了解更多 SQL 语法,请参考相关的 SQL 教程。

假设我们有一个 users 表,包含以下数据:

+----+------------+-----------+-----------------------+---------------------+
| id | first_name | last_name | email | registration_date |
+----+------------+-----------+-----------------------+---------------------+
| 1 | John | Doe | john.doe@example.com | 2023-10-26 10:15:00 |
| 2 | Mary | Moe | mary.moe@example.com | 2023-10-27 11:30:00 |
| 3 | Peter | Jones | peter.j@example.com | 2023-10-28 09:05:21 |
+----+------------+-----------+-----------------------+---------------------+

以下示例演示了如何使用 MySQLi 和 PDO 从 users 表中删除 id=3 的用户,同时使用预处理语句(prepared statements)以确保安全。

示例(MySQLi 面向对象风格及预处理语句):

Section titled “示例(MySQLi 面向对象风格及预处理语句):”
<?php
$servername = "localhost";
$username = "your_username"; // 替换为你的数据库用户名
$password = "your_password"; // 替换为你的数据库密码
$dbname = "my_database"; // 替换为你的数据库名
$userIdToDelete = 3;
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // 在生产环境中考虑更健壮的错误处理
}
// SQL 语句,包含占位符 (?)
$sql = "DELETE FROM users WHERE id = ?";
// 准备语句
$stmt = $conn->prepare($sql);
if ($stmt === false) {
die("Error preparing statement: " . $conn->error); // 基本错误检查
}
// 绑定参数 (s = string, i = integer, d = double, b = blob)
$stmt->bind_param("i", $userIdToDelete);
// 执行语句
if ($stmt->execute()) {
// 检查影响了多少行
if ($stmt->affected_rows > 0) {
echo "Record with ID $userIdToDelete deleted successfully.";
} else {
echo "No record found with ID $userIdToDelete or deletion failed.";
}
} else {
echo "Error executing statement: " . $stmt->error;
}
// 关闭语句和连接
$stmt->close();
$conn->close();
?>
<?php
$servername = "localhost";
$username = "your_username"; // 替换为你的数据库用户名
$password = "your_password"; // 替换为你的数据库密码
$dbname = "my_database"; // 替换为你的数据库名
$userIdToDelete = 3;
try {
// 创建 PDO 连接
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8mb4", $username, $password);
// 设置 PDO 错误模式为 exception,以获得更好的错误处理
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// SQL 语句,包含命名占位符 (:id)
$sql = "DELETE FROM users WHERE id = :id";
// 准备语句
$stmt = $conn->prepare($sql);
// 绑定参数(命名占位符)
$stmt->bindParam(':id', $userIdToDelete, PDO::PARAM_INT);
// 执行语句
$stmt->execute();
// 检查影响了多少行
$rowCount = $stmt->rowCount();
if ($rowCount > 0) {
echo "Record with ID $userIdToDelete deleted successfully ($rowCount row affected).";
} else {
echo "No record found with ID $userIdToDelete or deletion failed.";
}
} catch(PDOException $e) {
// 捕获并显示错误
echo "Error: " . $e->getMessage();
}
// 关闭连接(对 PDO 而言是可选的,脚本结束时自动发生)
$conn = null;
?>

为何使用预处理语句? 使用 ? (MySQLi) 或像 :id 这样的命名占位符 (PDO),然后绑定实际值,可以防止 SQL 注入攻击。数据库驱动程序会处理输入值的正确转义,使您的查询更安全。

任一脚本成功执行后,users 表将如下所示:

+----+------------+-----------+-----------------------+---------------------+
| id | first_name | last_name | email | registration_date |
+----+------------+-----------+-----------------------+---------------------+
| 1 | John | Doe | john.doe@example.com | 2023-10-26 10:15:00 |
| 2 | Mary | Moe | mary.moe@example.com | 2023-10-27 11:30:00 |
+----+------------+-----------+-----------------------+---------------------+