How to Delete a single Row or record from a mysql table using php

How to Delete a single Row or record from a mysql table using php

If you want to delete a record from any MySQL table,
then you can use SQL command DELETE FROM. You can use this in any scripting language like PHP.If WHERE clause is not specified, then all the records will be deleted from the given MySQL table.You can specify any condition in using WHERE clause.

You can delete records from a single table at a time.

The WHERE clause is very useful when you want to delete selected rows or record from a table.

Example of Deleting Data Using PHP Script:
You can use SQL DELETE command with or without WHERE CLAUSE into PHP function mysql_query().
This function will execute SQL command and delete row from the mysql table.

 < ?php
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'dbpassword';
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db('TUTORIALS') or die(mysql_error());

$sql = 'DELETE FROM tbl_student WHERE student_id=5';

$retval = mysql_query($sql);
if(! $retval )
  die('Could not delete data: ' . mysql_error());
echo "Deleted data successfully from student table\n";


About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.