"Call to a member function fetch_row() on a non-object" in spite of error handling
12,748
You are trying to fetch a row from a a query as an object, however the query that you are making is a DELETE which doesn't return values that can be fetch as a row, but a boolean (TRUE or FALSE) that is the result of the query being successful or not. In this case, $result should return the value TRUE or FALSE, that can be used like this for example:
<?php
$query = 'DELETE FROM `products` WHERE `company` ='.$id_nummer;
$results = $link->query($query);
if(!$results) {
echo $link->error;
} else {
echo "Company id:".$in_number." successfully deleted."
}
?>
Author by
Jacky Rank
Updated on June 04, 2022Comments
-
Jacky Rank almost 2 years
I want to execute a query but php/mysql throws
Call to a member function fetch_row() on a non-object
even though
if(!$results)
should filter out empty results. The error message points to
$row = $results->fetch_row();
This is the whole code:
<?php $query = 'DELETE FROM `products` WHERE `company` ='.$id_nummer; $results = $link->query($query); if(!$results) { echo $link->error; } else { $row = $results->fetch_row(); $wanted_result = $row[0]; } ?>
Where is the cause for this?
EDIT: I solved it by replacing
if(!$results)
with
if(!is_object($results))
and it works.
-
Jacky Rank over 11 yearsWhat is the advantage of using a prepared statement over my approach?
-
Michael Berkowski over 11 years@JackyRank The advantage is always security, as you don't require sanitization of the input (though you may still benefit from validation) see also stackoverflow.com/questions/732561/…
-
Jacky Rank over 11 yearsGood. See here: stackoverflow.com/questions/14323476/…
-
Michael Berkowski over 11 years@JackyRank I'm glad to see you're trying it out. The effort up front may seem large, but you get used to the few extra lines of code and it really pays off in security. (and the first answer there is correct - you ought not quote the placeholders
?
not'?'
. -
Jacky Rank over 11 yearsThat leads to "Fatal error: Call to a member function bind_param() on a non-object"