0

我需要有关 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>';?>
4

2 回答 2

1

如果three queries可以在single query

尝试这个,

<?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

于 2013-09-04T08:27:25.543 回答
0
while ($row = mysqli_fetch_array($query)) { 
    $product_id = $row["product_id"];
} 
var_dump($product_id);

上面只会得到30个product_id中的最后一个product_id,我猜你的意思是把它们推到这样的数组中

while ($row = mysqli_fetch_array($query)) { 
    $product_id[] = $row["product_id"];
} 
var_dump($product_id);

并且您在以下代码中有一些错误,请尝试以这种方式修复它们

于 2013-09-04T08:57:46.933 回答