Delete records using PHP MySQL Statement

April 2nd, 2011 by Laeeq | 1 comment

We use the DELETE statement to delete records in a table.

Syntax

DELETE FROM table_name

WHERE some_column = some_value

Please note the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Example

Look at the following “student” table:

FirstName LastName Age

Garry Kristen 25

Jhon Don 22

The following example deletes all the records in the “student” table where LastName=’Kristen’:

<?php

con = mysql_connect(“localhost”,”username”,”password”);

if (!$con)

{

die(‘Could not connect: ‘ . mysql_error());

}

mysql_select_db(“schooldatabase”, $con);

mysql_query(“DELETE FROM student WHERE LastName=’Kristen’”);

mysql_close($con);

?>

 

After the deletion, the table will look like this:

FirstName LastName Age
Jhon Don 22

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Create a CSV file using PHP/MYSQL
  • Php Interview Questions and Answers – Part 6
  • PHP MySQL Introduction
  • Import mysql database command line, Import MySQL dumpfile, SQL datafile into my database,
  • Update records using PHP MySQL Statement
  • Sort record set using PHP MySQL ORDER BY Statement
  • Extract Specified records using WHERE clause PHP/MYSQL
  • Select Records using PHP MySQL Select Statement
  • Insert a New Records in a Table – PHP MySQL
  • PHP MySQL – Create Database and Tables
  • Create Connection to MySQL Database
  •  

     

    1. online casino free bets
      December 10th, 2012 at 07:29 | #1

      I enjoy what you guys are up too. Such clever function and exposure! Keep up the really excellent works guys I’ve incorporated you guys to my own blogroll.

    1. No trackbacks yet.