user3453021
user3453021

Reputation: 93

Trying to get property of non-object

I want to if there are no authors/users found exit given the message below. But keep striking Trying to get property of non-object on the $author_found_count. Why is this? Thanks

$find_author = "SELECT user FROM reviews WHERE review_id=$review_id;";

$search_author = mysqli_query($con,$find_author);

$found_author = mysqli_fetch_array($search_author);

$author_found_count = $found_author->num_rows;

//Check to see if any reviews have been found.
if($author_found_count == 0) {

//No reviews found.
exit ("You are not the author of the review. You are not authorised to delete it.");
}

Upvotes: 1

Views: 363

Answers (3)

potashin
potashin

Reputation: 44611

You are trying to get the num_rows of the php array, but for this purpose you should use mysqli_result object:

$author_found_count = $search_author->num_rows;

You can also use php function to check if the result set is empty:

$author_found_count = count($found_author);

Upvotes: 0

SaidbakR
SaidbakR

Reputation: 13552

Because it is an array and should be accessed like that: review the documentation: https://www.php.net/manual/en/mysqli-result.fetch-array.php

Upvotes: 0

Basit
Basit

Reputation: 1840

It is because num_rows is not a property of your result set. Try $author_found_count = $search_author->num_rows; instead.

Upvotes: 1

Related Questions