lrave1 lrave1 - 2 months ago 13
PHP Question

PHP Error Message - Trying to get property of non-object

I keep getting the error


Trying to get property of non-object


in my PHP code. Here is a snippet of the code where the error is occurring.

This is the line the error log refers to:
$high = $highrow->price;


$mysqli = new mysqli($server, $user, $pass, $db);
$highresult = $mysqli->query("SELECT * FROM $code WHERE date=$date AND time=$hour ORDER BY price desc LIMIT 1");
$highrow = $highresult->fetch_object();
$high = $highrow->price;
echo $high." - High<br>";
$hightime = $highrow->time;
echo $hightime."<br>";


Am I missing something?

Answer
$mysqli = new mysqli($server, $user, $pass, $db);
$highresult = $mysqli->query("SELECT * FROM $code WHERE date='$date' AND time='$hour' ORDER BY price desc LIMIT 1");
$highrow = $highresult->fetch_object();
$high = $highrow->price;
echo $high." - High<br>";
$hightime = $highrow->time; 
echo $hightime."<br>";

Edit your select query as above

SQL requires single quotes around text values (most database systems will also allow double quotes).