【问题标题】:Trying to get property 'num_rows' of non-object even though var_dump returns an object试图获取非对象的属性“num_rows”,即使 var_dump 返回一个对象
【发布时间】:2019-08-28 16:27:05
【问题描述】:

所以我是 OOP 概念的新手。我决定以面向对象的方式调用我的数据库。我得到的错误是:

试图获取非对象的属性“num_rows”(在第 83 行 *参考第 83 行)

我了解错误消息,但无法找出其中的问题所在。我尝试了以下链接,但不幸的是,它们都没有进一步帮助我,或者我无法理解它们的含义。

Notice: Trying to get property 'num_rows' of non-object in line 35

Error - Trying to get property 'num_rows' of non-object

Get property num_rows of non-object

Trying to get property 'num_rows' of non-object

这就是我故意提出重复问题的原因,希望我的问题是其他帖子中尚未(尚未)解决的问题。

require 'connection.php';


//class DatabaseQueries handles all the queries required regarding CRUD.
class DatabaseQueries
{

    //the SQL string
    private $sql;

    // The connection -> completegallery
    private $conn;



    public function __construct($conn) 
    {

        $this->conn = $conn;

    }


    // function will check whether email already exists or not.
    protected function getEmailExistance(string $email)
    {

        //create the SQL
        $this->sql = $this->conn->prepare("SELECT * FROM userinfo WHERE email = ?");

        //bind the parameter to it (preventing sql injection this way)
        $this->sql->bind_param('s', $email);

        // $result = the execution of the SQL.
        $result = $this->sql->execute();
        $resultCheck = $result->num_rows; //* line 83
        var_dump($result); // returns boolean true.
        var_dump($this->sql); // returns a mysqli stmt object

        //check whether $resultCheck > 0
        //if yes, that would mean the userName already exists.
        if (!empty($result) && $resultCheck > 0) 
        {

            exit('should show something');

        } else 
        {           
            exit('always fails here');

        }
    }

} // ending class DatabaseQueries

我如何称呼 DatabaseQueries 类:

class Base extends DatabaseQueries
        {

            private $email;
            private $userName;
            private $name;
            private $lastName;
            private $pwd;
            private $pwdConfirm;

// here is the code where I check and assign the user input to the variable $email etc. 

            //this method is for test purposes only and will be removed after the website is 'done'.
            public function getEverything()
            {

                //link to check whether email is being used or not
                $this->getEmailExistance($this->email);
            }
}

我如何调用对象等

$userInformation = new base($conn);
$userInformation->setEmail($_POST['registerEmail']);
     //some other info, but not relevant to the problem.

我已经检查过我是否拼错了任何东西,但事实并非如此。 connection.php 中的连接也被声明为正确。

【问题讨论】:

  • 这确实是重复的,但很难找到。因为此类问题通常被关闭为“由错字引起的题外问题。”。 $result 变量不包含任何接近 mysqli stmt 的内容,并且永远无法拥有 num_rows 属性。因为它总是布尔值

标签: php mysqli


【解决方案1】:

正如 cmets 中提到的,execute() 不会返回结果,您必须 fetch() 它。这是应该允许你做你所要求的代码。

// function will check whether email already exists or not.
protected function getEmailExistance(string $email)
{

    //create the SQL
    $this->sql = $this->conn->prepare("SELECT * FROM userinfo WHERE email = ?");

    //bind the parameter to it (preventing sql injection this way)
    $this->sql->bind_param('s', $email);

    // $result = the execution of the SQL.
    $this->sql->execute(); <---- No need for variable here, its boolean
    $result = $this->sql->store_result(); <---- The result
    $num_rows = $this->sql->num_rows; <---- This will contain your row count

    if (!empty($result)) 
    {
        // fetch your results here
    } else 
    {           
        exit('always fails here');
    }
}

【讨论】:

  • 感谢您的努力,但它并没有起到作用。我拿走了您保留 '$resultCheck > 0 部分的部分(假设您忽略了这部分并且没有将其取出)。但它仍然无法查询。 $result 确实永远不会为空,但是当我检查 num_rows 是否大于 0 时,它会失败。
  • “失败”是什么意思? $num_rows$result 的输出是什么?
  • 啊,是的,我很抱歉不清楚。我的意思是:$result 的输出 (var_dump) 告诉我:object(mysqli_result) { ["num_rows"]=&gt; int(1) } 当我插入数据库中已经存在的值时这是正确的,但是当我 var_dump $num_rows 的结果时,它返回一个 INT(0) .还是我在这里错过了什么?
  • 好吧,不熟悉mysqli。我使用PDO,因为它看起来更直接和通用。但是在阅读了更多内容之后,我相信您必须在获得num_rows 之前存储结果,如docs 所示。我已经更新了我的答案。
猜你喜欢
  • 2019-11-17
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2014-12-09
相关资源
最近更新 更多