MySQL查询执行失败

时间:2019-01-27 10:23:02

标签: php mysql

为什么此代码在数据库中不起作用?

$sql = 
UPDATE users_information INNER JOIN users ON users.id = user_id SET following_count = :following_count WHERE user_id = :user_id;

$ data:

["following_count" => "following_count + 1" , "user_id" => "1273"]

执行return false

2 个答案:

答案 0 :(得分:0)

following_count + 1表达式,您不能绑定表达式,只能绑定值。因此,将查询重写为:

$sql = "
UPDATE users_information INNER JOIN users ON users.id = user_id 
SET following_count = following_count + 1 
WHERE user_id = :user_id";

$data中,您仅传递user_id

$data = ["user_id" => "1273"];

此外,我不清楚使用联接的意义是什么,为什么不进行简单查询:

$sql = "
UPDATE users_information 
SET following_count = following_count + 1 
WHERE user_id = :user_id";

答案 1 :(得分:0)

[“ following_count” =>“ following_count + 1”,“ user_id” =>“ 1273”]。

  

“ following_count +1”是一个字符串。

将其设为整数。或者,如果它是一个字符串,则在数据库中更改类型 作为varchar。

数据应类似于以下内容:

$following_count = $following_count + 1;
["following_count" => "some_number(from following_count value)" , "user_id" => "1273"]