我在尝试执行我的php脚本时遇到“致命错误:调用boolean in ...中的成员函数fetch_array()”错误。有问题的代码在这里:
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;
}
以下行是问题所在:
while($row = $backup->fetch_array(MYSQLI_ASSOC))
上述功能之前的代码如下:
function clearBackupPrices()
{
global $mysqli;
$clean = "TRUNCATE TABLE oc_product_price_backup";
$doclean = $mysqli->query($clean);
if(!$doclean) return $mysqli->error;
else return true;
}
我研究并用同样的问题研究了其他答案,但没有解决它的运气。有人对我的问题有任何建议吗?提前谢谢大家。
答案 0 :(得分:8)
从php documentation开始,MySQLi :: query()将:
失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或 EXPLAIN查询mysqli_query()将返回一个mysqli_result对象。对于 其他成功的查询mysqli_query()将返回TRUE。
这意味着以下查询失败(因此使$backup = FALSE
而不是解释错误陈述的对象):
$mysqli->query($bup);
这又意味着sql语句$bup
导致错误。我建议您查看它和您的表格。似乎错误不是语法错误(因为语法错误会导致更早的错误消息),这意味着MySQL可以读取您的语句,但由于某种原因操作失败。您必须检查您的SQL语句以及您的表,并查看逻辑中的缺陷。