Home » MySQL UPDATE Query Syntax with Example [Update Query in MySQL]
MySQL

MySQL UPDATE Query Syntax with Example [Update Query in MySQL]

Hello Developers, In this tutorial we will discuss MySQL UPDATE Query Syntax with Example [Update Query in MySQL]. You can use this query and update your database row or column data. I will explain the update process from MySQL/phpMyAdmin, Command Prompt and by using PHP. So, you can understand the process and use it on your project. Just keep reading this tutorial.

MySQL UPDATE Query

First of all, let’s start with syntax.

MySQL UPDATE Query Syntax

The following syntext is used to modify the data in MySQL.


UPDATE table_name
SET column1 = 'value1', column2 = 'value2', column3 = 'value3',...
WHERE column_name = value

  • Replace your table name with table_name.
  • You can update multiple column values after SET. Like: column1 = ‘value1’, column2 = ‘value2’,…
  • This query is affect only one table.
  • Where condition is used to specify the column or row value which you want to update.

Update Query in MySQL from phpMyAdmin

You can fire the update command from phpMyAdmin. Just follow the steps below.

Go to phpMyAdmin

First of all, login to your cpanel and click the phpMyAdmin. Then you have to click SQL tab at the top-center of the page. See the screenshot below.

Update Query in MySQL from phpMyAdmin

Write Query

Next step is to write update query inside the textarea and click “Go”. When you click “Go” the query will fire and update your data.

Fire/Write MySQL UPDATE Query from phpMyAdmin

UPDATE query in PHP

You can fire MySQL UPDATE Query inside the PHP function. The following example code will show you how to write update query in PHP.


<?php 

$DB_Host = 'localhost';
$DB_Name = "database_name"
$DB_Username = 'database_username';
$DB_Password = 'database_password';
$con = mysql_connect($DB_Host,$DB_Username,$DB_Password) or die(mysql_errno());
$db = mysql_select_db("$DB_Name",$con) or die(mysql_error());
			
// UPDATE Query
$qry = '
UPDATE table_name
SET name = "John Patel"
WHERE id = "1"
';
$result = mysql_query($qry);

if(!result)
{
   die ('Error:: in update'.mysql_error());
}
else
{
  echo "Data Updated Successfully";
}

?>

  • Fix database connection.
  • Write the update query and execute it.

Above all code will update the name as “John Patel” where id is quual to 1.

Update using Command Prompt

You can update database value using command promt. I will show you how you can do it. Read following instructions.


root@host# mysql -u root -p password;
Enter password:*******

mysql> use Database_Name;
Database changed

mysql> UPDATE table_name 
-> SET name = 'John Patel' 
-> WHERE id = 3;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql>

You can use above all methods to update your database from MySQL. Also, you can use the PHP method to update query in MySQL and update your forms.

I hope this tutorial will help you. Share it if you like it.

Releated Articles: