Database Delete data using PHP


php Database Delete data using PHP PHP Course

How to delete the existing record from the mysql database using PHP ?

To delete the existing data from the database we first need to establish the database connection. If you didn't know how to establish the database connection , you can visit this article ⇨PHP database connection

Delete data from database using PHP mysqli function

For deleting the existing data from the database we just need to learn the query that how to write the delete query with where condition. We have to just pass the connection and delete query with where condition into the mysqli_query() function.

Delete Query Syntax

"DELETE FROM table_name WHERE condition" mysqli_query($connection , $query) Now we are going to delete the user's from the below users table.

Delete Query Program

Here in the above table there exists two users Ali and Umer. ⇨ We want to delete the user Umer. ⇨ We will use the unique identifier column 'id' in where condition. <?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); mysqli_query($conn,"DELETE FROM users WHERE id=2"); ?>Note ! If you don't write the WHERE condition , then all records will delete.

DELETE data using PHP mysqli function using Variables

<?php $conn = mysqli_connect('localhost', 'root', '', 'myfirstdatabase'); $delete_id = '2'; mysqli_query($conn,"DELETE FROM users WHERE id='".$delete_id."' "); ?>
php PHP Course Chapter Next »