Home >>MySQL Tutorial >PHP MySQL Delete

PHP MySQL Delete

PHP MySQL Delete

DELETE keyword is used basically to delete 1 or more than one records from the database table.even if we delete each and every row of a table then also the schema of the table remain un deleted. It,s necessary to use where clause in delete query, otherwise all the records will be deleted.

Syntax
DELETE FROM table_name WHERE some_column = some_value 

Note : Notice the WHERE clause in the DELETE syntax is must otherwise all records will be deleted! Eg 1 Previous records in empInfo table are:

Emp_id Name Email Mobile
1 devesh devesh@gmail.com 9910099100
2 deepak deepak@gmail.com 9210053520
3 ravi ravi@gmail.com 9810098100

  This example deleted one row from "empInfo" table

<?php
//connect database 
$con=mysqli_connect("localhost","root","","Employee") or die(mysqli_error());
	
//update values of empInfo table
$data="delete from empInfo WHERE email='devesh@gmail.com'";
mysqli_query($con,$data);			
 ?>

After deletion , the "empInfo" table will look like this:

Emp_id Name Email Mobile
2 deepak deepak@gmail.com 9210053520
3 ravi ravi@gmail.com 9810098100

In the above example , empInfo table has 3 records of devesh,deepak, and ravi. we need to delete a record from this table. The record of devesh here is to be deleted. here first we create connection with the database,then database is selected using mysql_select_db(), then delete query is passed to the mysql_query and the table row is deleted.


No Sidebar ads