MYSQL - UPDATE Query

MYSQL UPDATE Query is used to update existing records in a table.

  • You can modify one or more field altogether.
  • You can use any condition using the WHERE clause.
  • You can update the values in a single table at a time.

The WHERE clause is very useful when you want to update the selected rows in a table.


MYSQL Syntax :

UPDATE table_name
SET column1 = value1, column2 = value2, column3 = value3, ...
WHERE condition;

MYSQL Query Example :
UPDATE tblstudent SET stud_name = 'manish patel', stud_address = 'ahmedabad, gujarat, india' WHERE tblstudent.stud_id = 1;
 

Shell command script:
Also you can run update query using mysql command Prompt.
[root@host]# mysql -u root -p
Enter password:********

mysql> use aryatechno;
Database changed

mysql> UPDATE tblstudent SET stud_name = 'manish patel', stud_address = 'ahmedabad, gujarat, india' WHERE tblstudent.stud_id = 1;
Query OK, 1 row affected (0.03 sec)



PHP SCRIPT :
You can update any records into table using php code as per as below.

<?php
$hostname="localhost";
$username="root";
$password="********";
$link= mysqli_connect($hostname, $username, $password);
mysqli_select_db($link,DATABASE);
$returnval = mysqli_query($link,"UPDATE tblstudent SET stud_address = 'ahmedabad, india' WHERE tblstudent.stud_id = 1");
 
if($returnval ){
        echo "Data is updated successfully.";
}else{
       die('MySQL Error : ' . mysqli_error($link));
}
mysqli_close($link);

?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

79998