MySQL UPDATE query
If we need to modify or update the data in MySQL, we can use SQL UPDATE command to operate. .
grammar
The following is the UPDATE command to modify the MySQL Data Sheet Data General SQL syntax:
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]
- You can update one or more fields simultaneously.
- You can specify any condition in the WHERE clause.
- You can also update the data in a separate table.
When you need to update the data specified in the table rows WHERE clause is very useful.
Command prompt to update the data
Below we will update w3big_tbl specified in the table data using SQL UPDATE command WHERE clause:
Examples
The following example will update the data table as w3big_title w3big_id field value 3:
root@host# mysql -u root -p password; Enter password:******* mysql> use w3big; Database changed mysql> UPDATE w3big_tbl -> SET w3big_title='Learning JAVA' -> WHERE w3big_id=3; Query OK, 1 row affected (0.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql>
Use PHP script to update data
PHP function for use mysql_query () to execute SQL statements, you can use SQL UPDATE statement or WHERE clause does not apply.
This function is in the mysql> command prompt effect executing SQL statements is the same.
Examples
The following example will update w3big_id data w3big_title field 3.
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'UPDATE w3big_tbl SET w3big_title="Learning JAVA" WHERE w3big_id=3'; mysql_select_db('w3big'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not update data: ' . mysql_error()); } echo "Updated data successfully\n"; mysql_close($conn); ?>