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

标签 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>';?>

最佳答案

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

试试这个,

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

相关文章:

php - Javascript:带 id 的链接

MySQL数据库组织

java - 日期值设置为 NULL 时的原始 SQL 更新语句

java - 查找二维数组中每一列的最大值

php mysql登录/注册当前登录用户不同的代码

php - 尝试获取非对象的属性(CodeIgniter)

php - 在 foreach 语句中使用 array_keys() 或设置一个从未使用过的值变量会更快吗?

php - 根据用户输入过滤结果

javascript - php json 到 javascript json 格式不符合要求的结果

python - Numpy astype 四舍五入到错误的值