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 insert a date into a MySQL database using PHP?
- How to output XML from MySQL using PHP?
- How to run multiple queries in PHP and MySQL?
- How to use a variable in a MySQL query 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?
- What port to connect to MySQL from PHP?
- How to use the LIKE operator in PHP and MySQL?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
See more codes...