douyan1970
2016-04-07 11:08
浏览 146
已采纳

未定义的属性:PDO :: $ affected_rows

after editing my old code to new i get error Undefined property: PDO::$affected_rows and here is the part if ($this->dbCon->affected_rows > 0) can some one help me to solve this problem

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

}}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjiaolia97750 2016-04-07 11:12
    已采纳

    A PDO object has no affected_rows property. Instead you can call rowCount() on the PDOStatement:

    if ($resultObj->rowCount() > 0) {
    

    Side note: not all databases support the rowCount() method for SELECT queries, but MySQL does.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题