MariaDB update query

UPDATE command to modify existing fields by changing the value. It uses the SET clause specifies the columns you want to modify, and specify a new value assigned. These values can be expressions or the default value of the field.
Set the default values need to use the DEFAULT keyword. The condition commands can also be used to specify the WHERE clause of the update and / or update the ORDER BY clause in a particular order.

Review the following general syntax -

UPDATE table_name SET field=new_value, field2=new_value2,...
[WHERE ...]

From a command prompt or execute UPDATE command using PHP script.

Command Prompt

At the command prompt, simply use the standard commandroot -

root@host# mysql -u root -p password;
Enter password:*******
mysql> use PRODUCTS;
Database changed
mysql> UPDATE products_tbl
   SET nomenclature = 'Fiber Blaster 300Z'
      WHERE ID_number = 112;
mysql> SELECT * from products_tbl WHERE ID_number='112';
+-------------+---------------------+----------------------+
| ID_number   | Nomenclature        | product_manufacturer |
+-------------+---------------------+----------------------+
| 112         | Fiber Blaster 300Z  | XYZ Corp             |
+-------------+---------------------+----------------------+

PHP script to update queries

Use the UPDATE command statement mysql_query () function -

<?php
   $dbhost = ‘localhost:3036’;
   $dbuser = ‘root’;
   $dbpass = ‘rootpassword’;
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die(‘Could not connect: ‘ . mysql_error());
   }

   $sql = ‘UPDATE products_tbl
      SET product_name = ”Fiber Blaster 300z”
      WHERE product_id = 112’;

   mysql_select_db(‘PRODUCTS’);
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die(‘Could not update data: ‘ . mysql_error());
   }

   echo “Updated data successfully
”;
   mysql_close($conn);
?>

After a successful update the data, you will see the following output -

mysql> Updated data successfully

This switched: http: //codingdict.com/article/7097

Guess you like

Origin www.cnblogs.com/bczd/p/12009707.html