PHP致命错误:无法将mysqli_result类型的对象用作file.PHP中的数组
我有以下代码: $productQuery = "SELECT ean, name FROM products WHERE category = '$category'"; $productResult = $mysqli->query($productQuery); while ($productRow = $productResult->fetch_assoc()) { $productPriceQuery = "SELECT ean, price+shipmentCost AS totalPrice FROM prices WHERE ean = $productRow[ean] ORDER BY totalPrice ASC"; $productPriceResult = $mysqli->query($productPriceQuery); $totalPrice = $productPriceResult->fetch_assoc(); echo $productPriceResult["productURL"]; } 但是我得到了回音线的以下错误: PHP Fatal error: Cannot use object of type mysqli_result as array in file.php 我已经在本网站和其他网站上阅读了一些文章,但不了解问题所在,自己也无法解决。$productPriceResult是一个结果,而不是一个获取的数组 似乎productURL应该存储在products表中,但为了解释起见,我将按您的方式保存它:在prices表中 您需要将productURL添加到SELECT语句中,如下所示:PHP致命错误:无法将mysqli_result类型的对象用作file.PHP中的数组,php,mysql,mysqli,Php,Mysql,Mysqli,我有以下代码: $productQuery = "SELECT ean, name FROM products WHERE category = '$category'"; $productResult = $mysqli->query($productQuery); while ($productRow = $productResult->fetch_assoc()) { $productPriceQuery = "SELECT ean, price+shipm
SELECT ean, productURL, price+shipmentCost AS totalPrice FROM prices ...
$totalPrice['productURL'];
$totalPrice['totalPrice'];
并按如下方式访问productURL:
SELECT ean, productURL, price+shipmentCost AS totalPrice FROM prices ...
$totalPrice['productURL'];
$totalPrice['totalPrice'];
顺便提一下,价格将如下所示:
SELECT ean, productURL, price+shipmentCost AS totalPrice FROM prices ...
$totalPrice['productURL'];
$totalPrice['totalPrice'];
我建议将$totalPrice变量重命名为$priceData或更合适的名称
编辑:
下面是一个如何循环查看价格数据的示例。
为了可读性,我截断了实际的查询字符串
// loop through rows fetched from "products" table
while ($productRow = $productResult->fetch_assoc()) {
// query price data for this product
$productPriceQuery = "SELECT ean, productURL, etc...";
$productPriceResult = $mysqli->query($productPriceQuery);
// loop through rows fetched from "prices" table
while ($priceData = $productPriceResult->fetch_assoc()) {
echo "<p>".$priceData['productURL']."</p>";
}
}
试用
public function DisplayProducts() {
$productQuery = "SELECT ean, name FROM products WHERE category = '$category'";
$productResult = $this->mysqli->query($productQuery);
while ($productRow = $productResult->fetch_object()) {
$productPriceQuery = "SELECT ean, price+shipmentCost AS totalPrice FROM prices WHERE ean = $productRow->ean ORDER BY totalPrice ASC";
$productPriceResult = $this->mysqli->query($productPriceQuery);
while($row=$productPriceResult->fetch_object()) {
$ProductPrice = $row->totalPrice;
$ProductURL = $row->productURL; //I'm not seeing where you're querying for the URL so you'll have to append accordingly.
}
}
然后创建您正在谈论的表
echo "<table><th>Product Price</th><th>Product URL</th>";
echo "<tr><td>" . $ProductPrice . "</td><td>" . $ProductURL . "</td></tr>";
echo "</table>";
}
希望有一些帮助,从哪里开始。祝您好运。似乎$productPriceResult是sql结果,而不是获取的数组。productURL是否存储在products表中?它存储在表“prices”中。使用mysqli时,应使用参数化查询,并将用户数据添加到查询中。避免使用字符串插值来实现这一点。愚蠢的我:我确实必须使用$totalPrice而不是$productPriceResult。现在它部分地起作用了。表中有5条记录,3条ean 123和2条ean 567。但是只给出了一个结果。不,不愚蠢——计算机只是不太理解;。哪个部分没有工作?或者作为一个单独的帖子更好?你是指价格表吗?如果是这样,则每个产品只能从该表中提取一次。仅获取返回的第一行。要获取更多行,您需要一个类似于第一次查询时使用的while循环的循环。我在回答中添加了一个示例来说明第二个循环。请大家帮个忙,如果您要投反对票,请至少告诉大家您的理由: