7

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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nancy
  • 504
  • 2
  • 6
  • 21
  • 1
    I believe I found the problem. check out my answer – Webeng May 26 '16 at 08:19
  • Can you add the line `if(!$backup) print_r($mysqli->error);` and show us any output... – ImClarky May 26 '16 at 08:37
  • Unknown column 'p.price_sync' in 'field list' - the output I got after adding the line as suggested. @ImClarky – Nancy May 26 '16 at 09:23
  • @Nancy Well that should give you the info you need. Either the column `price_snyc` doesnt exist in the `oc_product` table, or there is something like a typo... – ImClarky May 26 '16 at 09:29
  • @ImClarky Now that i added th missing columns in table, I get the following error: Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in .../class.smtp.php on line 338 – Nancy May 26 '16 at 09:49
  • 1
    @Nancy - check out [this question](http://stackoverflow.com/q/26827192/3760604). See if that helps :) – ImClarky May 26 '16 at 09:56

1 Answers1

13

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.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • Thanx for your reply, I will follow your recommendation and see where it leads me. @ImClarky - Thanx for your reply too. I will try and post the output. – Nancy May 26 '16 at 09:12
  • Thank you for your help, you were both right and it helped me a lot to resolve my issue. I truly appreciate it! @ImClarky The error was to be found at $mysqli->query($bup); After I printed the error, it showed some of the columns were missing. So I added them and after some permission issues(certain files did not have right permissions), everything worked fine. – Nancy May 27 '16 at 06:10