Fatal error: call to a member function fetch_array() on boolean

匿名 (未验证) 提交于 2019-12-03 07:36:14

问题:

I am getting the "Fatal error: call to a member function fetch_array() on boolean in..." error when trying to execute my php script. The code in question is here:

function backup() {     global $mysqli;      $bup        = "SELECT p.product_id, p.ean, p.image, p.model,  p.status, p.price_sync, p.modified_by, p.date_modified, pd.name, pd.description, pd.language_id, pd.meta_description, pd.meta_keyword, pd.tag FROM oc_product p INNER JOIN oc_product_description pd ON p.product_id = pd.product_id";     $backup     = $mysqli->query($bup);     $megainsert = "REPLACE INTO oc_product_backup(product_id, ean, image, model,  status, price_sync, modified_by, date_modified, name, description, language_id, meta_description, meta_keyword, tag) VALUES ";      while($row  = $backup->fetch_array(MYSQLI_ASSOC))     {         $product_id       = $mysqli->real_escape_string($row['product_id']);         $ean              = $mysqli->real_escape_string($row['ean']);         $image            = $mysqli->real_escape_string($row['image']);         $model            = $mysqli->real_escape_string($row['model']);         $name             = $mysqli->real_escape_string($row['name']);         $description      = $mysqli->real_escape_string($row['description']);         $meta_description = $mysqli->real_escape_string($row['meta_description']);         $meta_keyword     = $mysqli->real_escape_string($row['meta_keyword']);         $tag              = $mysqli->real_escape_string($row['tag']);          $megainsert      .= "('".$product_id."', '".$ean."', '".$image."', '".$model."',  '".$row['status']."', '".$row['price_sync']."', '".$row['modified_by']."', '".$row['date_modified']."', '".$name."', '".$description."', '".$row['language_id']."', '".$meta_description."', '".$meta_keyword."', '".$tag."'),";     }      $backup->close();     $megainsert = substr_replace($megainsert, "", -1);     $dobackup   = $mysqli->query($megainsert);     if(!$dobackup) return $mysqli->error;     else return true; } 

the following line is where the problem is:

while($row  = $backup->fetch_array(MYSQLI_ASSOC)) 

The code right before the function above is as follows:

   function clearBackupPrices() {     global $mysqli;      $clean   = "TRUNCATE TABLE oc_product_price_backup";     $doclean = $mysqli->query($clean);     if(!$doclean) return $mysqli->error;     else return true; } 

I researched and looked into other answers with the same question, but had no luck resolving it. Does anyone have a suggestion for my problem, please? Thank you all in advance.

回答1:

From the php documentation, MySQLi::query() will:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

This means that the following query is failing (and hence making $backup = FALSE rather than an object which explains your error statement):

$mysqli->query($bup); 

Which in turn means that the sql statement $bup is causing an error. I recommend reviewing it and your table. It seems the error is not a syntax error (since a syntax error would have caused an even earlier error message), which means that MySQL can read your statement, but the operation is failing for some reason. You'll have to review your SQL statement as well as your table and see what the flaw in the logic is.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!