Notice – trying to get property num_rows of non-object in php

num_rows of non-object is the most common error in PHP. This type of error occurs whenever you write MySQL SELECT Query in Object-Oriented way to fetch some from the database. Before fetching data you check the total number of records using num_rows. So. Sometimes you get an error.

Now, you should not worry about this type of error, you have found the right tutorials to solve it. I have also got this type of error many times. So, I have solved it and have executed my query.

I will tell you everything step by step that How have I get an error & How have I solve it. So, read all the given points everything, you will easily solve this error. Even you will get a new concept to solve an error in PHP and MYSQL

trying to get property num_rows of non-onject

 Notice: Trying to Get Property num_rows of non-object in PHP

First of all, I am going to show the following query that had been produced a Notice error:num_rows of non-object.

<?php

$hostname     = "localhost";
$username     = "root";
$password     = ""; 
$databasename = "codingstatus"; 

// Create connection 
$db = new mysqli($hostname, $username, $password,$databasename);
if ($db->connect_error) { 
die("Unable to Connect database: " . $db->connect_error);
 }
 
 $tableName='users'; 
 $data=[
  'email'=>'example@gmail.com',
  'password'=>'2bfab85fd7097d6b089ac9d6a3f622db'
 ];

// call function
exist_user($data,$tableName);

// declare function
function  exist_user($data,$tableName){

       global $db;
       $query="SELECT * from".$tableName."WHERE email='".$data['email']."' AND password='".$data['password']."'";
      $result=$db->query($query);
      if ($result->num_rows> 0) { 
         $fetchUserData = $result->fetch_assoc(); 
         print_r($fetchUserData);
      }else{
          echo "No record found";
      }
  }
?>

 

I have got the following error from the above script.

Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\codingstatus\user-login\test.php on line 30

I have checked my query many times but This error was not solved. At last, I was beginning to apply the condition then It was solved. So, Read the next step, How I did it.

  • I have put $result in if condition and echo error query echo "Error in ".$query."<br>".$db->error; like the following query.
<?php

$result=$db->query($query);
      if($result){
      if ($result->num_rows> 0) {
          
         $fetchUserData = $result->fetch_assoc(); 
         print_r($fetchUserData);
      }else{
          echo "No record found";
      }
    }else{
      echo "Error in ".$query."<br>".$db->error;
    }
?>

Then I have got the following error message

Error in SELECT * fromusersWHERE email='example@gmail.com' AND password='2bfab85fd7097d6b089ac9d6a3f622db'
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'fromusersWHERE email='example@gmail.com' AND password='2bfab85fd7097d6b089ac9d6a' at line 1
  • In the above error message. I have got an error in my SQL syntax near the table name. There is no space among FROM, users table name & WHERE clause.
  • When I have put a single white space among FROM, users table name & WHERE clause. then I have got another following error.
Error in SELECT * from users WHERE email='example@gmail.com' AND password='2bfab85fd7097d6b089ac9d6a3f622db'
Unknown column 'password' in 'where clause'

In the above error message. I have got that the password column is not created in the users’ table. I have created this column and again run it. my query works well.

Hence, I solved this error by doing a few steps. Now, you are thinking that the same error does not come in your query. I know It may not. You may get other mistakes in your query. So, I will tell you general ways to solve it from the next steps. Follow those steps. you will definitely solve your error.

How to Solve

From the previous steps, You have seen How to solve this type of error Notice: trying to get num_rows property of non-object. Previous steps are most important to learn the next step. So, I am going to learn you the best way.

  • Check your query that it is written in the proper format or not.
  • You should check the following things in your SQL statement.
  • Single whitespace among words of SQL statement
  • Right table name as created in the database
  • Single or double white space
  • the semicolon at the end of the statement
  • This type of error occurs only if your query is not written in proper syntax. So, you have to put your MySQL query result in if condition. Then Put num_rows property within the if block and print $db->error property within the else block.
 <?php 
$query="Your query statement here"; 
$result=$db->query($query); 
if($result){ 
 if ($result->num_rows> 0) {
   echo "Record was found"; 
 }else{ 
   echo "No record found";
 } 
}else{ 
echo "Error in ".$query."
".$db->error; }
 ?>

 

  • The above condition will show you a clear error message on your web page. you will easily read & improve greeted errors.

Read Also

Load More data on Infinite Scroll Using Ajax in PHP

PHP Interview Questions and Answers

My Suggestion

I hope you like this tutorial. Even it is helpful for you. So, Don’t forget to share with friends those who want to learn it.

If you have any queries, you can ask me through the below comment box. I will also share more coding tutorials. So you should continue to visit my blog for becoming an expert in the coding field.