SQL错误:在非对象上调用成员函数fetch_assoc()

时间:2016-05-19 13:48:15

标签: php mysqli

我对此代码有疑问:

if($sql = $this->database->query("SELECT * FROM warenkorb_artikel WHERE WarenkorbID = '".$erg['WarenkorbID']."'"))
{
    while($erg = $sql->fetch_assoc()) 
    {
        [...]
    }                    
}

我收到错误:

  

致命错误:在非对象上调用成员函数fetch_assoc()   在[...]中,“while($ eg = $ sql-> fetch_assoc())”是。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您需要分解,以便捕获您在mysqli API返回的查询中所犯的任何错误。

$sql = "SELECT * 
        FROM warenkorb_artikel 
        WHERE WarenkorbID = '{$erg['WarenkorbID']}'";

$result = $this->database->query($sql);

if ( $result === false ) {
    echo $this->database->error;
    exit;
}

// do you really need a while loop, as it look like you might be
// selecting a row using a unique key?

while($row = $result->fetch_assoc()){
         [...]
}