从两个表中选择数据,返回数据并从项目ID中获取产品名称

时间:2012-02-17 20:09:19

标签: php mysql

<?php
$transactionOutput = "";
$sql = mysql_query("SELECT * FROM transactions WHERE emailaddress='$email'");
$productCount = mysql_num_rows($sql);
if($productCount > 0) {
while($row = mysql_fetch_array($sql)) {
    $item_id = $row["item_id"];
    $quantity = $row["quantity"];
    $size = $row["size"];
    $price = $row["price"]; 

    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id'");
    $productCount = mysql_num_rows($sql);
    while($row = mysql_fetch_array($sql)) {
    $product_name = $row["product_name"];
    }

    $transactionOutput .= "<tr>";
    $transactionOutput .= "<td align='center'>" .$product_name. "</td>";
    $transactionOutput .= "<td align='center'>" .$quantity. "</td>";    
    $transactionOutput .= "<td align='center'>" .$size. "</td>";
    $transactionOutput .= "<td align='center'>" .$price. "</td>";
    $transactionOutput .= "</tr>";
}

    } else {
$transaction_list = "You have made no transactions yet";
}


?>

我试图从两个不同的表中访问数据,然后通过将product表中的id与transactions表中返回的item_id相匹配来返回每个项的产品名称。这确实输出了正确的信息,但是它只显示了第一笔交易而没有其他交易,我知道这也可能是非常可编程的

2 个答案:

答案 0 :(得分:1)

问题似乎是您使用相同的变量$sql来存储两个查询的结果集。那么这里可能发生的是:

  1. 运行事务查询,结果集存储在$ sql
  2. 从事务结果集中读取第一条记录
  3. 对于第一个事务记录,产品查询运行,结果集再次存储在$ sql
  4. 输出存储在$ transactionOutput
  5. 从事务结果集中读取下一条记录
  6. 并且步骤5是问题,因为原始事务结果集 - $sql - 被产品结果集覆盖。

    尝试将另一个变量用于产品查询:

    $rsProduct = mysql_query("SELECT * FROM products WHERE id='$item_id'");
        $productCount = mysql_num_rows($rsProduct);
        while($row = mysql_fetch_array($rsProduct)) {
        $product_name = $row["product_name"];
    }
    

    希望以上有意义!

    编辑:作为补充建议,您可以尝试使用JOIN查询在同一查询中检索事务和产品。这里:

    SELECT `t`.*, `p`.`product_name`
    FROM `transactions` `t`
    LEFT JOIN `products` `p` ON `t`.`item_id` = `p`.`id`
    WHERE `t`.`emailaddress` = '$email';
    

    只需循环结果集即可完成!

答案 1 :(得分:0)

<?php
$transactionOutput = "";
$sql = mysql_query("SELECT * FROM transactions WHERE emailaddress='$email'");
$productCount = mysql_num_rows($sql);
if($productCount > 0) {

while($row = mysql_fetch_array($sql)) {
    $item_id = $row["item_id"];
    $quantity = $row["quantity"];
    $size = $row["size"];
    $price = $row["price"]; 

    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id'");
    $productCount = mysql_num_rows($sql);

   #I suggest you use a different variable (i.e. not $row) here;
    #at this point $row from the first while is still in scope
    #and clobbering it may be causing the problem you see with
    #only the first transaction showing.        
    while($row = mysql_fetch_array($sql)) {
     #If you only need the first product_name, then you don't need the loop, just
     #$row2=mysql_fetch_arrqy($sql);
     #$product_name = $row2["product_name"];

    $product_name = $row["product_name"];
    }

    $transactionOutput .= "<tr>";
    $transactionOutput .= "<td align='center'>" .$product_name. "</td>";
    $transactionOutput .= "<td align='center'>" .$quantity. "</td>";    
    $transactionOutput .= "<td align='center'>" .$size. "</td>";
    $transactionOutput .= "<td align='center'>" .$price. "</td>";
    $transactionOutput .= "</tr>";
}

    } else {
$transaction_list = "You have made no transactions yet";
}


?>