Trying to get property 'num_rows' of non-object even though var_dump returns an object

So I am new to the OOP concept. I decided to make a call to my database, but in OO way. The error I am getting is:

Trying to get property 'num_rows' of non-object (at line 83 *reference to line 83)

I understand the error message, but fail to find out what is wrong regarding it. I have tried the following links, but unfortunately none of them have helped me really further, or I failed to understand what they meant.

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

This is the reason I am knowingly making a duplicate question, hoping my problem would be something that has not (yet) been addressed in the other posts.

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

How I call the class 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);
            }
}

How I invoke the objects etc.

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

I have already checked whether I misspelled anything, but this wasn't the case. The connection in connection.php has been declared correct aswell.

Upvotes: 0

Views: 281

Answers (1)

EternalHour
EternalHour

Reputation: 8621

As mentioned in the comments, execute() does not return the result, you have to fetch() it. Here is the code that should allow you to do what you are asking.

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

Upvotes: 0

Related Questions