-1
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "comp4";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 


$sql = "SELECT OrderID, id, items FROM orders WHERE id= $user";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$results =   $row["OrderID"]. " " . $row["id"]. $row["items"]. "<br>" ;

}

$loop = implode( " ", $items );

echo $loop;
}
?>

So, I have this code and I'm trying to display OrderID, id and items from the user thats logged in $user=whoever's logged in that's in a different part of my code, however I keep getting an error Notice: Trying to get property of non-object in C:\xampp\htdocs\myorders.php on line 35 After looking around, I'm still not quite sure how to fix this. Any help is appreciated

Bah
  • 29
  • 1
  • 3

1 Answers1

0

I'm guessing your error lies in this line of code:

$sql = "SELECT OrderID, id, items FROM orders WHERE id= $user";

It could be that ID is a reserved keyword of SQL, or that $user is undefined and/or not a numeric value.

Try the following:

$sql = "SELECT `OrderID`, `id`, `items` FROM `orders` WHERE id = $user";

The reason for the error is your query fails. Then you later do the following:

if ($result->num_rows > 0) {

And since the query failed, $result is not an object, and you try to access num_rows of this non-object.

Of course I am only guessing because we need more information :)

beiller
  • 3,105
  • 1
  • 11
  • 19