将我的旧代码编辑为新版后,我收到错误Undefined property: PDO::$affected_rows
,这里的部分if ($this->dbCon->affected_rows > 0)
可以帮助我解决这个问题
class Relation {
private $loggedInUser;
private $dbCon;
public function getRelationship(User $user) {
$user_one = (int) $this->loggedInUser->getUserId();
$user_two = (int) $user->getUserId();
if ($user_one > $user_two) {
$temp = $user_one;
$user_one = $user_two;
$user_two = $temp;
}
$resultObj = $this->dbCon->prepare('SELECT * FROM relationship WHERE user_one_id=:user_one AND user_two_id=:user_two');
$resultObj->execute(array(':user_one' => $user_one,':user_two' => $user_two));
if ($this->dbCon->affected_rows > 0) {
$row = $resultObj->fetch(PDO::FETCH_ASSOC);
$relationship = new Relationship();
$relationship->arrToRelationship($row, $this->dbCon);
return $relationship;
}
return false;
}
}}
答案 0 :(得分:5)
PDO对象没有affected_rows
属性。相反,您可以在PDOStatement上调用rowCount()
:
if ($resultObj->rowCount() > 0) {
附注:并非所有数据库都支持SELECT查询的rowCount()
方法,但MySQL确实如此。