You can use SQL’s DELETE FROM command to delete records in the MySQL datasheet.
You can do it in the The following is the general syntax for the SQL DELETE statement to delete data from the MySQL data table: If no WHERE clause is specified, all records in the MySQL table are deleted. You can specify any condition in the WHERE clause You can delete records in a single table at once. The WHERE clause is very useful when you want to delete a specified record in a data table. Here we will use the WHERE clause in the SQL DELETE command to delete the data selected by the MySQL data table runoob_tbl. The following instance will be deleted DELETE statement: PHP usage This function is similar to the The following PHP instances will be deleted MySQL DELETE clause test:
mysql>
Execute the command at a command prompt or in a PHP script.Grammar ¶
DELETE FROM table_name [WHERE Clause]
Delete data from the command line ¶
Example ¶
runoob_tbl
In the table
runoob_id
A record of 3:mysql>useRUNOOB;Databasechangedmysql>DELETEFROMrunoob_tblWHERErunoob_id=3;QueryOK,1rowaffected(0.23sec)
Use PHP scripts to delete data ¶
mysqli_query()
Function to execute the SQL statement, you can use it in the SQL DELETE command or not
WHERE
Clause.
mysql>
The command executes the SQL command with the same effect.Example ¶
runoob_tbl
In the table
runoob_id
A record of 3:<?php$dbhost='localhost';//mysql服务器主机地址$dbuser='root';//mysql用户名$dbpass='123456';//mysql用户名密码$conn=mysqli_connect($dbhost,$dbuser,$dbpass);if(!$conn){die('连接失败:'.mysqli_error($conn));}//设置编码,防止中文乱码mysqli_query($conn,"set
names utf8");$sql='DELETE FROM runoob_tbl WHERE
runoob_id=3';mysqli_select_db($conn,'RUNOOB');$retval=mysqli_query($conn,$sql);if(!$retval){die('无法删除数据:'.mysqli_error($conn));}echo'数据删除成功!';mysqli_close($conn);?>