php-mysqlHow to delete data from a MySQL database using PHP?
To delete data from a MySQL database using PHP, the DELETE statement can be used. The following example code block shows how to delete a row from a table named users where the id is 1:
$sql = "DELETE FROM users WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
The output of the example code would be:
Record deleted successfully
The code consists of the following parts:
$sql: This is a variable that contains theDELETEstatement.$conn->query($sql): This is a function that executes theDELETEstatement.ifstatement: This is a conditional statement that checks if theDELETEstatement was executed successfully.echostatement: This is a function that prints out a message depending on the result of theDELETEstatement.
Helpful links
More of Php Mysql
- How to get the version of MySQL using PHP?
- How to use a MySQL union in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to get the first row of a result in MySQL using PHP?
- How to check the result of an insert in PHP and MySQL?
- How to get the last insert ID in PHP MySQL?
- How to export data from MySQL to Excel using PHP?
- How to call a stored procedure in MySQL using PHP?
- How to return multiple rows as an array in MySQL using PHP?
See more codes...