2

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Jacky Rank
  • 85
  • 1
  • 1
  • 6

2 Answers2

0

You cannot fetch results from a DELETE query, as there are no rows to fetch. The contents of $results is a boolean true or false, based on success or failure of the DELETE.

In your case, it is TRUE, and your if (!$results) evaluates to FALSE, sending you into the else.

$results itself will tell you whether or not the action succeeded (whether the query was syntactically valid), but $link->affected_rows will tell you if you actually managed to match and delete any rows.

if ($result && $link->affected_rows > 0) {
  // You managed to delete something
}
else if ($result) {
  // successful query, no rows deleted
}
else {
  // Error
  echo $link->error;
}

Since you appear to be using MySQLi, consider creating a prepared statement instead of query() for this.

$stmt = $link->prepare("DELETE FROM `products` WHERE `company` = ?");
$stmt->bind_param('i', $id_nummer);
$stmt->execute();

// Then check affected rows
if ($stmt->affected_rows > 0) {
  // Successful deletion of some rows...
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • What is the advantage of using a prepared statement over my approach? – Jacky Rank Jan 14 '13 at 16:26
  • @JackyRank The advantage is always security, as you don't require sanitization of the input (though you may still benefit from validation) see also http://stackoverflow.com/questions/732561/why-is-using-a-mysql-prepared-statement-more-secure-than-using-the-common-escape – Michael Berkowski Jan 14 '13 at 16:42
  • Good. See here: http://stackoverflow.com/questions/14323476/number-of-variables-doesnt-match-number-of-parameters-yes-they-do – Jacky Rank Jan 14 '13 at 17:29
  • @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 `'?'`. – Michael Berkowski Jan 14 '13 at 17:31
  • That leads to "Fatal error: Call to a member function bind_param() on a non-object" – Jacky Rank Jan 14 '13 at 17:42
0

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."
}
?>
david
  • 3,225
  • 9
  • 30
  • 43
BeoWulf
  • 627
  • 2
  • 6
  • 18