問題描述
我找到了很多方法來使用 PDO 的 exec 語句,但我不確定它對我有幫助.我的理解是我必須對準(zhǔn)備好的語句使用 execute() 函數(shù).我正在使用來自用戶輸入的數(shù)據(jù)更新一行,因此我想使用準(zhǔn)備好的語句而不是 query() 調(diào)用.
I have found many ways to use the exec statement for PDO, but I'm not sure it helps me. My understanding is that I have to use the execute() function for prepared statements. I am updating a row with data from user input, so I would like to use a prepared statement instead of the query() call.
我的代碼如下:
$dbh = buildDBConnector();
$sql = "UPDATE tb_users
SET authState=1
WHERE id = ? AND authPass = ?";
$q = $dbh->prepare($sql);
$f = $q->execute(array($id,$authPass));
if($f){
echo '<br />Success<br />';
}else{
echo '<br />Failure<br />';
}
問題是查詢本身沒有錯誤并且執(zhí)行得很好,所以沒有存儲在 $f 中的失敗.但是,我需要知道它是否真的找到了要更新的行,然后成功更新了它.換句話說,我需要受影響的行.在谷歌搜索等時,它不斷出現(xiàn) exec 語句,但據(jù)我所知, exec 不是用于準(zhǔn)備好的語句?有什么建議嗎?
The issue is that the query itself is error free and executes fine, so there is no failure to store in $f. However, I need to know if it actually found the row to update, then successfully updated it. In other words, I need the affected rows. When googling and such, it keeps coming to the exec statement, but from my understanding, exec isn't for prepared statements? Any suggestions?
推薦答案
試試 $q->rowCount()
.準(zhǔn)備好的語句將通過該方法返回受影響的行數(shù).
Try $q->rowCount()
. Prepared statements will return the number of affected rows via that method.
這篇關(guān)于PDO 是執(zhí)行語句期間受影響的行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!