問題描述
我已閱讀此線程:問題遞增MySQL/PHP 中的一個(gè)字段,帶有準(zhǔn)備好的語句,但沒有看到我的問題的答案.
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' ) );
據(jù)我所知,這一切都是正確的.
As far as I can tell, all this is correct.
當(dāng)上面的代碼執(zhí)行時(shí),不管notif列的值是什么,它都會(huì)將notif列設(shè)置為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));
您不能將列名綁定到準(zhǔn)備好的語句.
You can't bind a column name to a prepared statement.
這篇關(guān)于使用 PDO Prepared Statement 并遞增列值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!