<?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";
}
?>
I’m trying to access data from two different tables and then return the product name of each item by matching the id in the product table with the item_id returned from the transactions table. This does output the correct information however it only shows the first transaction and no others, i know this is also probably horribly programmed too
The problem seems to be that you are using the same variable
$sqlto store the resultsets from both the queries. So what might be happening here is this:AND the step 5 is the problem because the original transaction resultset –
$sql– was overwritten by the product resultset.Try using another variable for the product query:
Hope the above makes sense!
EDIT: as an additional suggestion, you may like to try using JOIN queries to retrieve both transaction and product in the same query. Here:
Just loop the resultset and you are done!