DELETE 語句用於從數據庫表中刪除行。php
刪除數據庫中的數據
DELETE FROM 語句用於從數據庫表中刪除記錄。html
語法
DELETE FROM table_name
WHERE some_column = some_value
WHERE some_column = some_value
註釋:請注意 DELETE 語法中的 WHERE 子句。WHERE 子句規定了哪些記錄須要刪除。若是您想省去 WHERE 子句,全部的記錄都會被刪除!mysql
如需學習更多關於 SQL 的知識,請訪問咱們的 SQL 教程。sql
爲了讓 PHP 執行上面的語句,咱們必須使用 mysqli_query() 函數。該函數用於向 MySQL 鏈接發送查詢或命令。數據庫
實例
請看下面的 "Persons" 表:函數
FirstName | LastName | Age |
---|---|---|
Peter | Griffin | 35 |
Glenn | Quagmire | 33 |
下面的實例刪除 "Persons" 表中全部 LastName='Griffin' 的記錄:學習
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
mysqli_close($con);
?>
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"DELETE FROM Persons WHERE LastName='Griffin'");
mysqli_close($con);
?>
在此次刪除後,"Persons" 表以下所示:spa
FirstName | LastName | Age |
---|---|---|
Glenn | Quagmire | 33 |