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 theDELETE
statement.$conn->query($sql)
: This is a function that executes theDELETE
statement.if
statement: This is a conditional statement that checks if theDELETE
statement was executed successfully.echo
statement: This is a function that prints out a message depending on the result of theDELETE
statement.
Helpful links
More of Php Mysql
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to join tables with PHP and MySQL?
- How to get the version of MySQL using PHP?
- How to write an update query in MySQL using PHP?
- How to count the number of resulting rows in a MySQL database using PHP?
- How to use a MySQL union in PHP?
- How to set a timeout for MySQL query in PHP?
- How to create an SSL connection to MySQL using PHP?
- How to get the last insert ID in PHP MySQL?
- How to fetch data from MySQL in PHP?
See more codes...