Lệnh DELETE (MySQL)
Có thể bạn quan tâm
- Trang chủ
- Lập trình PHP
- Lệnh DELETE (MySQL)
Trong hướng dẫn này, bạn sẽ học cách xóa bản ghi khỏi bảng MySQL bằng PHP.
Xóa dữ liệu bảng cơ sở dữ liệu
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:
DELETEFROMtable_nameWHEREcolumn_name=some_valueHã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 | [email protected] | | 2 | John | Rambo | [email protected] | | 3 | Clark | Kent | [email protected] | | 4 | John | Carter | [email protected] | | 5 | Harry | Potter | [email protected] | +----+------------+-----------+----------------------+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
Ví dụ
<?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
Ví dụ
<?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
Ví dụ
<?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, bảng persons sẽ trông giống như sau:
+----+------------+-----------+----------------------+ | id | first_name | last_name | email | +----+------------+-----------+----------------------+ | 1 | Peter | Parker | [email protected] | | 3 | Clark | Kent | [email protected] | | 5 | Harry | Potter | [email protected] | +----+------------+-----------+----------------------+Như 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.
Bài viết này đã giúp ích cho bạn?
Có Không Trang trước Trang sau AdvertisementsTừ khóa » Xóa Dữ Liệu Trong Sql Bằng Php
-
Bài 10: Delete Dữ Liệu MySQL Bằng PHP - Freetuts
-
Delete Dữ Liệu Từ MySQL Trong PHP - Học Lập Trình PHP Online
-
Delete Dữ Liệu Trong Database Bằng PHP | Lập Trình Từ Đầu
-
Bài 07: Delete Dữ Liệu MYSQL Trong PHP - Vi
-
Xóa (delete) Dữ Liệu Trong MySQL Với PHP - Góc Học IT
-
Câu Lệnh DELETE | MySQL & PHP
-
Hướng Dẫn Xóa Dữ Liệu Bằng MySQLi - Tự Học Lập Trình PHP
-
Mệnh đề DELETE Xóa Dữ Liệu Trong SQL
-
Bài 27: Delete Dữ Liệu MySQL Bằng PHP - Học Lập Trình PHP Cơ Bản
-
Tạo Chức Năng Sửa Xoá Thành Viên Bằng PHP & MySQL
-
Xóa Dữ Liệu Trong Sql Bằng Php | Vượt-dố
-
Xóa Dữ Liệu Trong Bảng MySQL Bằng PHP? [bản Sao] - HelpEx
-
MySQL Delete | Hướng Dẫn Học PHP | Học Web Chuẩn
-
Xóa Dữ Liệu Từ MySQL Database Sử Dụng PHP