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

san*_*nce -1 php mysql pdo

编辑我的旧代码后,我得到错误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;
    }
Run Code Online (Sandbox Code Playgroud)

}}

MrC*_*ode 5

PDO对象没有affected_rows属性.相反,你可以调用rowCount()PDOStatement:

if ($resultObj->rowCount() > 0) {
Run Code Online (Sandbox Code Playgroud)

附注:并非所有数据库都支持rowCount()SELECT查询的方法,但MySQL确实如此.