PHP MySQL: Delete

This entry is part 7 of 17 in the series PHP & MySQL

Before issuing delete command using PHP, let’s check that you have the proper environment and that there are records in the table artist using Toad.

inserted data

The query statement we use to delete is

delete from artist where id = 1

We pass the above query to a variable $sql using

$sql = "delete from artist where id = 1"

The full PHP script is as below.  As with the earlier insert example, we pass the connection string and the above delete string to mysqli_query.


$mysqli = new mysqli("localhost", "root", "", "allartist");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL";

$sql = "delete from artist where id = 1";

if (mysqli_query($mysqli, $sql)) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting data from table with error no: " . $mysqli->errno;


If deletion is successful, we will see the browser display as below.


We can also verify that the record is successfully deleted using Toad.


Series Navigation<< PHP MySQL: Insert Record
PHP MySQL: Update, non OOP style >>

Leave a comment

Leave a Reply