update命令通过更改值来修改现有字段。 它使用set子句指定要修改的列,并指定分配的新值。 这些值可以是字段的表达式或默认值。 设置默认值需要使用default关键字。 该命令还可以使用where子句来指定更新的条件和/或order by子句以特定顺序更新。
查看以下一般语法 -
update table_name set field=new_value, field2=new_value2,... [where ...]
从命令提示符或使用php脚本执行update命令。
在命令提示符下,只需使用标准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 | +-------------+---------------------+----------------------+
在update命令语句中使用mysql_query()函数 -
<?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); ?>
成功数据更新后,您将看到以下输出 -
mysql> updated data successfully