doulu1325 2013-01-14 15:58
浏览 102
已采纳

“在非对象上调用成员函数fetch_row()”尽管有错误处理[关闭]

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.

  • 写回答

2条回答 默认 最新

  • dongyuanliao6204 2013-01-14 16:07
    关注

    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."
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?