Mục lục
Xóa dữ liệu trong bảng
Cũng giống như bạn chèn bản ghi vào bảng, bạn có thể xóa bản ghi khỏi bảng bằng câu lệnh SQL DELETE . Nó thường được sử dụng trong phép liên hợp với mệnh đề WHERE để chỉ xóa những bản ghi phù hợp với tiêu chí hoặc điều kiện cụ thể.
Cú pháp cơ bản của câu lệnh DELETE:
DELETE FROM table_name WHERE column_name=some_value
Hãy tạo một truy vấn SQL bằng cách sử dụng câu lệnh DELETE và mệnh đề WHERE, sau đó chúng ta sẽ thực hiện truy vấn này thông qua việc chuyển nó đến hàm mysqli_query( ) để xóa các bản ghi bảng. Hãy xem xét bảng persons sau bên trong cơ sở dữ liệu demo:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | peterparker@mail.com | | 2 | John | Rambo | johnrambo@mail.com | | 3 | Clark | Kent | clarkkent@mail.com | | 4 | John | Carter | johncarter@mail.com | | 5 | Harry | Potter | harrypotter@mail.com | +----+------------+-----------+----------------------+
Mã PHP trong ví dụ sau sẽ xóa bản ghi của những người đó khỏi bảng persons có first_name đầu tiên bằng John.
Thủ tục
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if(mysqli_query($link, $sql)){
echo "Records were deleted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
Hướng đối tượng
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "root", "", "demo");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// Attempt delete query execution
$sql = "DELETE FROM persons WHERE first_name='John'";
if($mysqli->query($sql) === true){
echo "Records were deleted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close();
?>
PDO
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
try{
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
// Attempt update query execution
try{
$sql = "DELETE FROM persons WHERE first_name='John'";
$pdo->exec($sql);
echo "Records were deleted successfully.";
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close connection
unset($pdo);
?>
Sau khi xóa dữ liệu, bảng persons sẽ trông giống như sau:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | peterparker@mail.com | | 3 | Clark | Kent | clarkkent@mail.com | | 5 | Harry | Potter | harrypotter@mail.com | +----+------------+-----------+----------------------+
Như vậy bạn có thể thấy, các bản ghi đã được xóa thành công khỏi bảng persons.
Cảnh báo : Mệnh đề WHERE trong lệnh DELETE quy định cụ thể bản ghi sẽ bị xóa. Nếu bạn bỏ qua mệnh đề WHERE, tất cả các bản ghi sẽ bị xóa.







