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

时间:2013-09-04 08:11:43

标签: php mysql arrays

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

上面只会得到30个product_ids的最后一个product_id,我想你的意思是将它们推入这样的数组

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

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