pTutorial

New way to learn PHP

PHP 5 MYSQL update clause

How to used update clause

Update statement is used to modified or update the existing records into the database.


Suppose there is a company database that store the employs record like name, id, salary etc. One of the employ has been promoted and his salary incresed at this position update statement is required.

Before updation

Id Name Salary
1152 John 20000

After updation

Id Name Salary
1152 John 30000

Syntax

UPDATE table_name
SET column1=value, column2=value2, ...

Syntax

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Complete example of UPDATE table data

<?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 updation<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="UPDATE student
set name ='victor'  where name = 'xyz'";
$a=mysql_query($query,$db);
echo"<br>After updation<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 updation
------------------------
1
john
Ece
------------------------
------------------------
2
xyz
ma
------------------------
------------------------
3
harry
cse
------------------------
updated

After updation
------------------------
1
john
Ece
------------------------
------------------------
2
victor
ma
------------------------
------------------------
3
harry
cse
------------------------

QUOTES OF THE DAY

Wonder is the first cause of philosophy.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER