0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    This is indeed a duplicate but hard to find one. Because such questions are usually closed as "Off topic questions caused by a typo.". The $result variable doesn't contain anything close to a mysqli stmt and will never be able to have num_rows property. Because it's always boolean – Your Common Sense Aug 28 '19 at 16:44

1 Answers1

0

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');
    }
}
EternalHour
  • 8,308
  • 6
  • 38
  • 57
  • Appreciate the effort, but it did not do the job. I took away the part where you kept the '$resultCheck > 0 part (assuming you overlooked this part and didn't take it out). But it still fails the query. $result is indeed never empty, but when I check whether the num_rows are bigger than 0, it fails. – I try so hard but I cry harder Aug 28 '19 at 17:47
  • What do you mean by "it fails?" What is the output of `$num_rows` and `$result`? – EternalHour Aug 28 '19 at 17:50
  • Ah yes, my apologies for not being clear. What I meant was: The output (var_dump) of $result tells me: `object(mysqli_result) { ["num_rows"]=> int(1) }` which is correct when I insert a value that's already in the database, but when I var_dump the result of $num_rows, it returns an INT(0). Or am I missing out on something here? – I try so hard but I cry harder Aug 28 '19 at 18:12
  • Ok, not familiar with `mysqli`. I use `PDO` because it seems more straight forward and versatile. But after reading up more on it, I believe you have to store the result before getting `num_rows` as indicated in the [docs](https://www.php.net/manual/en/mysqli-stmt.num-rows.php). I have updated my answer. – EternalHour Aug 28 '19 at 18:22