Using PDO Prepared Statement and Incrementing a column value(使用 PDO Prepared Statement 并递增列值)
问题描述
我已阅读此线程:问题递增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 Statement 并递增列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PDO Prepared Statement 并递增列值


- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01