0
我会尽我所能来说明情况查询多行,但是我是相当新的PHP和MySQL这样的IM生锈的一点点。从存储结果
什么IM特异性想要实现导致客户已购买“码”回声查询。其链接到order_list分贝,其中列出他们所购买的产品:当客户的订单不止一个项目,每个给予客户订单号(1081例所示)
我的问题所在。因此,在这个例子中1081订购3个产品(1,11,14)这显示在数据库..
od_id | pd_id
----------------
1081 | 1
1081 | 11
1081 | 14
所以,当我成功地查询1081它带回了3行的结果。这对于这个例子和数量查询来说很好,但是当我需要所有三个结果时,我只能得到一个结果返回到最终的sql查询。
这里是我的测试代码至今:
//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 />";
}
在页面上目前的结果是这样的:
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.
我希望我是有道理的,基本上我需要的最后的“$ sqlcode”查询来处理前一个查询中的所有行,而不仅仅是第一个。
谢谢!
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
感谢这样做的一种尝试。这似乎已经奏效,但打印出来后阵(见测试现场页)我看到的数量仍然在读的第一个结果(所以我得到两个结果每个,而不是1对1,1对11和2为14)。我原本考虑SQL连接,但它似乎压倒我,如果整个脚本可以在一个$ sql中语句来完成我会考虑再次。 – 2011-04-18 12:20:57
添加了一些代码来显示连接场景 – bumperbox 2011-04-18 23:43:24