Forum Moderators: coopster

Message Too Old, No Replies

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

         

Gilead

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

10+ Year Member



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();

LifeinAsia

5:11 pm on Jun 20, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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...

Dimitri

5:20 pm on Jun 20, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



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.

Gilead

5:53 pm on Jun 20, 2019 (gmt 0)

10+ Year Member



The echo for $sql came out right. Will update again shortly.

Gilead

6:04 pm on Jun 20, 2019 (gmt 0)

10+ Year Member



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.

Dimitri

10:36 am on Jun 23, 2019 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



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()"