pTutorial

New way to learn PHP

PHP 5 MYSQL delete clause

How to used delete clause

Delete clause is uded to remove the data present in table(database).

Syntax

The given query delete all the record in that table

DELETE From table_name

The given query delete selected record in that table

DELETE From table_name
WHERE some_column=some_value

Password field are blank because we did not choose any password

Example of delete statement



<?php

$user = "root";
$pwd = "";
$host = "localhost";
$db = @mysql_connect($host,$user,$pwd);
mysql_select_db("my_db1");
$data2="select *from student";
$data3=mysql_query($data2)or die(mysql_error());
echo "Before deletion<br>";
while($row = mysql_fetch_array($data3)) 
{
	echo "------------------------<br>";
    echo $row['id']."<br>";
    echo $row['name']."<br>";
    echo $row['branch']."<br>";
    echo "------------------------";
    echo "<br>";
}
$query="DELETE from student
where id =2";
$a=mysql_query($query);
echo"<br>After deletion<br>";
$data="select *from student";
$data1=mysql_query($data)or die(mysql_error());

while($row1 = mysql_fetch_array($data1)) 
{
	echo "------------------------<br>";
    echo $row1['id']."<br>";
    echo $row1['name']."<br>";
    echo $row1['branch']."<br>";
    echo "------------------------";
    echo "<br>";
}
mysql_close($db);

?>

Output

Before deletion
------------------------
2
victor
ma
------------------------
------------------------
3 harry cse
------------------------
------------------------
4
john
me
------------------------
After deletion ------------------------
3
harry
cse
------------------------
------------------------
4
john
me
------------------------

Once records are deleted a SELECT is used to ensure their deletion took place as above shown. From line number 23 to 35 is used for reprint all the data of student table.


QUOTES OF THE DAY

Saying is one thing, and doing is another.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER