Mariadb 简明教程
MariaDB - Update Query
UPDATE 命令通过更改值来修改现有字段。它使用 SET 子句来指定用于修改的列,并指定分配的新值。这些值可以是表达式或字段的默认值。设置默认值需要使用 DEFAULT 关键字。该命令还可以使用 WHERE 子句为更新指定条件和/或 ORDER BY 子句以按特定顺序更新。
The UPDATE command modifies existing fields by changing values. It uses the SET clause to specify columns for modification, and to specify the new values assigned. These values can be either an expression or the default value of the field. Setting a default value requires using the DEFAULT keyword. The command can also employ a WHERE clause to specify conditions for an update and/or an ORDER BY clause to update in a certain order.
复习以下一般语法 −
Review the following general syntax −
UPDATE table_name SET field=new_value, field2=new_value2,...
[WHERE ...]
从命令提示符或使用 PHP 脚本执行 UPDATE 命令。
Execute an UPDATE command from either the command prompt or using a PHP script.
The Command Prompt
在命令提示符下,只需使用标准命令根 −
At the command prompt, simply use a 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 Update Query Script
在 UPDATE 命令语句中使用 mysql_query() 函数 −
Employ the mysql_query() function in UPDATE command statements −
<?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\n”;
mysql_close($conn);
?>
在成功更新数据后,您将看到以下输出 −
On successful data update, you will see the following output −
mysql> Updated data successfully