php 致命错误:在布尔值上调用成员函数 fetch_array()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37455186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:14:22  来源:igfitidea点击:

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

phpmysql

提问by Nancy

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:

我在尝试执行我的 php 脚本时收到“致命错误:调用成员函数 fetch_array() on boolean in...”错误。有问题的代码在这里:

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.

我研究并查看了相同问题的其他答案,但没有成功解决它。有人对我的问题有什么建议吗?谢谢大家。

回答by Webeng

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

php 文档中,MySQLi::query() 将:

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.

失败时返回 FALSE。对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象。对于其他成功的查询,mysqli_query() 将返回 TRUE。

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

这意味着以下查询失败(因此创建$backup = FALSE而不是解释您的错误语句的对象):

$mysqli->query($bup);

Which in turn means that the sql statement $bupis 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.

这反过来意味着 sql 语句$bup导致错误。我建议查看它和您的表格。似乎该错误不是语法错误(因为语法错误会导致更早的错误消息),这意味着 MySQL 可以读取您的语句,但由于某种原因操作失败。您必须检查您的 SQL 语句以及您的表,并查看逻辑中的缺陷。