gpt4 book ai didi

php - mysql 查询需要一些循环或数组

转载 作者:行者123 更新时间:2023-11-29 13:37:00 25 4
gpt4 key购买 nike

我需要 mysql 查询方面的帮助。我有多个具有相同 order_id 的产品,然后想向所有人展示,但它只显示 30 个产品中的一个产品。

if (!$_GET['orderid']) {

}
else
{
$order_id = ereg_replace("[^0-9]", "", $_GET['orderid']); // filter everything but numbers for security
}

$sqlCommand = "SELECT * FROM tabell_order_product WHERE order_id='$order_id'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$product_id = $row["product_id"];

}
mysqli_free_result($query);
//---------------------------------------------------------------------------------
$sqlCommand = "SELECT * FROM tabell_product WHERE product_id='$product_id'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$model = $row["model"];
$location = $row["location"];
}
mysqli_free_result($query);
//---------------------------------------------------------------------------------

$sqlCommand = "SELECT * FROM tabell_product_description WHERE product_id='$product_id'";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$name = $row["name"];
}
mysqli_free_result($query);
$output .= '<tr>
<td>' . $product_id . '</td>
<td>' . $name . '</td>
<td>' . $model . '</td>
<td>' . $location . '</td>
</tr>';?>

最佳答案

如果可以在单个查询中完成,为什么要调用三个查询

试试这个,

<?php
$sqlCommand = "SELECT t2.product_id,t2.name,t3.model,t3.location FROM
tabell_order_product t1, tabell_product t2, tabell_product_description t3
WHERE t1.order_id='$order_id' AND t2.product_id=t3.product_id";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$product_id=$row['product_id'];
$name=$row['name'];
$model=$row['model'];
$location=$row['location'];
$output .= '<tr>
<td>' . $product_id . '</td>
<td>' . $name . '</td>
<td>' . $model . '</td>
<td>' . $location . '</td>
</tr>';
}
?>

阅读mysqli-fetch-array

关于php - mysql 查询需要一些循环或数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com