使用PDO Prepared语句并增加列值 [英] Using PDO Prepared Statement and Incrementing a column value
本文介绍了使用PDO Prepared语句并增加列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已阅读此线程:问题正在递增MySQL/PHP中带有预准备语句的字段,但没有看到我的问题的答案.
I've read this thread: Issues incrementing a field in MySQL/PHP with prepared statements but didn't see the answer to my problem.
PDOStatement Object
(
[queryString] => UPDATE user_alerts SET notif = ? + 2 WHERE ( user_id = ? )
)
$stmt->execute( array( 'notif', '1' ) );
据我所知,所有这些都是正确的.
As far as I can tell, all this is correct.
执行上述代码时,无论notif列的值是多少,它都会将notif列设置为等于2.就像SQL读取SET notif = 2
而不是SET notif = notif + 2
When the above code executes, it sets the notif column equal to 2 irregardless of what the value of the notif column is. It's as if the SQL is reading like SET notif = 2
instead of SET notif = notif + 2
我无法弄清楚,非常感谢您的帮助!
I haven't been able to figure it out and would really appreciate help!
推荐答案
$sql = 'UPDATE user_alerts SET notif = notif + 2 WHERE ( user_id = :userid )';
$prepStatement = $pdo->prepare( $sql );
$prepStatement->execute(array(':userid' => $userid));
您不能将列名绑定到准备好的语句.
You can't bind a column name to a prepared statement.
这篇关于使用PDO Prepared语句并增加列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文