Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

mysqli

I am getting the error when trying to execute my php script:

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

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;
}
like image 598
Nancy Avatar asked May 26 '16 08:05

Nancy


1 Answers

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.

like image 141
Webeng Avatar answered Nov 13 '22 06:11

Webeng