PDO在执行语句期间受行影响 [英] PDO were rows affected during execute statement

查看:48
本文介绍了PDO在执行语句期间受行影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了许多用于PDO的exec语句的方法,但是我不确定它是否对我有帮助.我的理解是,我必须对准备好的语句使用execute()函数.我正在用用户输入的数据更新行,因此我想使用准备好的语句而不是query()调用.

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 />';
}

问题在于查询本身没有错误并且可以很好地执行,因此在$ f中存储没有错误.但是,我需要知道它是否确实找到了要更新的行,然后成功地对其进行了更新.换句话说,我需要受影响的行.当进行谷歌搜索时,它会不断出现在exec语句中,但是据我了解,exec是否不是针对准备好的语句的?有什么建议吗?

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() .准备好的语句将通过该方法返回受影响的行数.

Try $q->rowCount(). Prepared statements will return the number of affected rows via that method.

这篇关于PDO在执行语句期间受行影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆