开发者

querying multiple rows from stored results

i'll try my best to explain the situation but i'm fairly new to php and mysql so im a tad rusty.

What im specificity trying to achieve is a query resulting in the echo of "codes" that a customer has purchased.

My problem lies when a customer orders more then one item, each customer is given an order number (in the example: 1081) which links to a order_list db which lists which products they have purchased. So in this example 1081 ordered 3 products (1,11,14) this is shown in the database as..

od_id  |  pd_id
----------------
1081   |  1
1081   |  11
1081   |  14

So when i successfully query 1081 it brings back 3 rows of results. This is fine for this example and the quantity query but i only get one result back on the final sql query when i need all three results.

Here is my test code so far:

//Get order IDS
$sqlid = "SELECT pd_id FROM tbl_order_item WHERE od_id = 1081";
$result = mysql_query($sqlid);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlid;
die($message);
}
开发者_如何学运维
while($row = mysql_fetch_assoc($result)) {
$ids[] = $row['pd_id'];
}

foreach ($ids as $id) {
echo $id . "<br />";
}

//Get order Qtys
$sqlqty = "SELECT od_qty FROM tbl_order_item WHERE od_id = 1081";
$result = mysql_query($sqlqty);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlqty;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$qtys[] = $row['od_qty'];
}

foreach ($qtys as $qty) {
echo $qty . "<br />";
}

//Get Order Codes
$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT $qty";
$result = mysql_query($sqlcode);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlcode;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$codes[] = $row['od_code'];
} 

foreach ($codes as $code) {
echo $code . "<br />";
}

The current results on the page are this:

1   //Three results from "pd_id" (What the customer ordered)
11  //
14  //
1   //Three results from "od_qty" (How many the customer ordered
1   //
2   //
YYXY-YYXY-YYXY-YYXW   //The first code result with two codes as there was 2 qty for product 14
YYXY-YYXY-YYXY-YYXX   //

my test page you can view the above here.

I hope i made sense, basically i need the final "$sqlcode" query to process all the rows from the previous query not just the first.

Thanks!

Key:
pd_id //product id
tbl_order_item //ordered items table
od_id //order id 
od_qty //quantity of each product
od_code //order codes
tbl_order_code //codes table each code is related to the pd_id 


wrap the last sql in a foreach so that it gets executed for each $id

you should really read up on sql joins, as could do this entire script as one $sql stmt

foreach ($ids as $id) {

$sqlcode = "SELECT od_code FROM tbl_order_code WHERE pd_id = $id LIMIT $qty";
$result = mysql_query($sqlcode);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sqlcode;
die($message);
}

while($row = mysql_fetch_assoc($result)) {
$codes[] = $row['od_code'];
} 

}

here is an attempt at doing it with a join (if that helps any)

// query data
$order_id = 1081;

$sql = "SELECT tbl_order_item.pd_id, tbl_order_item.od_qty, tbl_order_code.od_code FROM tbl_order_item JOIN tbl_order_code ON 
tbl_order_code.pd_id=tbl_order_item.pd_id
WHERE tbl_order__item.od_id = $order_id";

$result = mysql_query($sql);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sql;
die($message);
}


while($row = mysql_fetch_assoc($result)) {
    printf("Order ID: %s - Product ID: %s - Qty: %s - Code: %s<br />", $order_id, $row['pd_id'], $row['od_qty'], $row['od_code']);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜