The second echo below throws the error “Trying to get property of non-object?” The first echo prints fine.
$sql = "SELECT id
FROM files
WHERE url = '$url'";
$result = $this->mysqli->query($sql);
echo "companyId: " . $result->fetch_object()->id;
echo "\ncompanyId: " . $result->fetch_object()->id;
I’ve doubled up the echo for simplicity of demonstrating this error. I’m actually trying to check if $result->fetch_object()->id is set and then do something with the value if it is. I can’t get that to work correctly because the second time i use it, it throws the error “trying to get property of non-object.”
if(isset($result->fetch_object()->id))
echo $result->fetch_object()->id;
The mysqli-resultsets works with cursors. This means, if you call
fetch_object()the object is fetched (of course), the cursor moves on and now the previously retrieved object not present in the resultset anymore. Retrieve the object into a variable and interact with that, instead of callingfetch_object()multiple times.