如何通过在数据库

时间:2015-05-27 08:07:20

标签: php mysql mysqli

假设在数据库中我有一个值2300,我想为它添加200,我有以下PHP脚本

$to_add = 200;
$query = mysqli_query("select * from table where id = '$row'");
$rec = mysqli_fetch_assoc($query);
$old_number = $rec['col'];
$new_number = $old_number + $to_add;
$query1 = mysqli_query("update table set col = '$new_number' where id = '$row' ");

我的问题是,我们可以使用单个查询完成上述任务,例如

$new_number = 200;
$query = mysqli_query("update table set col = 'pre_value + $new_number' where id = '$row' ");

3 个答案:

答案 0 :(得分:2)

还可以使用速记赋值运算符

$new_number = 200; $query = mysqli_query("update table set col +=
$new_number where id = '$row'");

答案 1 :(得分:1)

试试这个:

$new_number = 200;
$query = mysqli_query("update table set col = col + $new_number where id = '$row' ");

答案 2 :(得分:1)

$new_number = 200;

update table set col = col + $new_number where id = '$row';