Welcome to WebmasterWorld Guest from 3.80.4.76

Forum Moderators: coopster & jatar k

Trying to get property 'num rows' of non-object error

     
4:23 pm on Jun 20, 2019 (gmt 0)

Full Member

5+ Year Member

joined:Oct 19, 2011
posts: 201
votes: 1


Having some trouble here, could use some help!
I used this same exact code in another script and it worked fine. I copy and paste to a new script, try it out and it gives me the num_rows error. I do not understand. It's almost the same exact code as before. I thought it might be the query statement since I'm adding an id element to it, but I took it out, just to check and it did the same thing. Could really use an extra pair of eyes on this. Thanks!

include('dbconfig.php');
$id=$_POST['VehicleID'];
$location=$_POST['Location'];


$sql= "SELECT * FROM oilchange WHERE ID= '".$id."' and Location = '".$location."'";
$result=mysqli_query($ms, $sql);
if($result ->num_rows>0)
{
$rows=array();
while($row=$result->fetch_assoc())
{
$rows[]=$row;
}
echo json_encode($rows);
}
else
{
echo"No Oil Changes Found For This Vehicle!";
}

$ms->close();
5:11 pm on June 20, 2019 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5847
votes: 195


Print the value of $sql before the mysqli_query call to make sure it's what you expect. If so, then cut/paste the statement into MySQL Workbench (or whatever you use to run manual queries) and make sure you're actually getting results.

After the mysqli_query call, you can also print_r($result) to verify num_rows is there and see what the value is.

Also, I hope you're planning on doing some work to prevent SQL injection with that...
5:20 pm on June 20, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 284


mysqli_num_rows($result) instead of $result->num_rows

mysqli_fetch_assoc($result) instead of $reslt->fetch_assoc

OR, to keep your existing code

IF $ms is an object and not a mysql link., it's $ms->query(...) instead of mysqli_query.

You have to use procedural functions OR object oriented functions. You can't mix both.

Unrelated to your question, but following LifeinAsia comment :

- you must never use the $_POST data without verifying them,
- you must escape strings passed to MySQL,

- you should use "require" instead of "include" too.
5:53 pm on June 20, 2019 (gmt 0)

Full Member

5+ Year Member

joined:Oct 19, 2011
posts: 201
votes: 1


The echo for $sql came out right. Will update again shortly.
6:04 pm on June 20, 2019 (gmt 0)

Full Member

5+ Year Member

joined:Oct 19, 2011
posts: 201
votes: 1


Something is still wrong, I'm getting: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given.
So I inserted a >0, same error returned.

$sql= "SELECT * FROM oilchange WHERE ID= '".$id."' and Location = '".$location."'";
echo $sql;
$result=$ms->query($sql);
if(mysqli_num_rows($result)>0)
{
$rows=array();
while(mysqli_fetch_assoc($result))
{
$rows[]=$row;
}
echo json_encode($rows);
}


As to the sanitation of the POST vars, they are taken from a saved file and typed in as normal data. What is recommended for sanitation. My knowledge is very out of date.
10:36 am on June 23, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 284


You are still mixing object oriented functions and procedural ones.

If "$ms" is an object , then you use "$result->num_rows" and "$row=$result->fetch_assoc()"
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members