PHP MySQL Update

In SQL, Update is another statement which is used to update any record of a table.

PHP MySQL Update

PHP MySQL Update

     

In SQL, Update is another statement which is used to update any record of a table. This command is useful when we need to update any existing value, which could be wrong or need to be updated.

General format of the command is as follows:

update <table name>

set <field-name=value,....>

where <condition>

In the above syntax where is important because if you do not use this every field will be changed.

Now let us implement this statement in PHP.

Example:

<?php

$db=mysql_connect("localhost","root","");

if(!$db)

{

die("Cannot connect".mysql_error());

}

mysql_select_db("roseindia",$db);

echo "<br/>Before updation:<br/>";

$result=mysql_query("select * from student");

while($row=mysql_fetch_array($result))

{

echo $row['e_id']." ".$row['age']."<br/>";

}

echo"<br/>After updation:<br/>";

mysql_query("update student set age=26 where e_id='emp01'");

$result=mysql_query("select * from student");

while($row=mysql_fetch_array($result))

{

echo $row['e_id']." ".$row['age']."<br/>";

} 0

mysql_close($db);

?>

Output: 1


Before updation:
emp01 28
emp02 27
emp05 26
emp_05 28

After updation:
emp01 26
emp02 27
emp05 26
emp_05 28