我的代码有问题:
<?php
echo $s_country;
$sql2="SELECT prod_id FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$i=0;
$j="";
while($rs=mysql_fetch_array($res)){
$j = $rs['prod_id'];
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id=".$j;
$res=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($res);
}
$i++;
}
?>
我真正想要做的是从表tbl_order_item
中获取产品ID,并使用该产品ID从表tbl_product
中选择这些ID的运费。
例如,如果有两个产品ID,则应选择两个ID的运费。 但这里只适用于这样的一个产品ID:
SELECT shipping_us FROM tbl_product WHERE prod_id=526
但我要做的是:
SELECT shipping_us FROM tbl_product WHERE prod_id=526
SELECT shipping_us FROM tbl_product WHERE prod_id=527
答案 0 :(得分:3)
你可以使用OR,比如
SELECT shipping_us FROM tbl_product WHERE prod_id=526 OR prod_id=527
答案 1 :(得分:2)
你也可以像IN
SELECT shipping_us FROM tbl_product WHERE prod_id IN (526,527)
答案 2 :(得分:0)
第一个解决方案:
更改
$res=mysql_query($sql3) or die(mysql_error()); // you have overwritten result set of first query
$shipping_cost1=mysql_fetch_object($res);
要
$newres=mysql_query($sql3) or die(mysql_error());
$shipping_cost1=mysql_fetch_object($newres);
第二种解决方案(更好):
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
echo $row['shipping_us']
}
}
建议:
1.学会防止MySQL注入:Good Link
2.建议不要使用扩展名来编写新代码。相反,应该使用mysqli或PDO_MySQL扩展。更多阅读:PHP Manual
答案 3 :(得分:0)
我的pblm已经解决了......特别是GBD ..
这是我的最终代码......
<?php
echo $s_country;
$sql2="SELECT GROUP_CONCAT(prod_id SEPARATOR ',') as pid FROM tbl_order_item WHERE order_id='$order_id'";
$res=mysql_query($sql2) or die(mysql_error());
$shipping_cost1 = 0;
if(mysql_num_rows($res) > 0){
$rs=mysql_fetch_array($res);
if(trim($s_country)=='US' || trim($s_country=='United States' )){
$sql3 = "SELECT shipping_us FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
if(mysql_num_rows($res) > 0){
while ($row = mysql_fetch_array($res)){
$shipping_cost1 = $shipping_cost1 + $row['shipping_us'];
}
}
} else {
$sql3 = "SELECT shipping_outside FROM tbl_product WHERE prod_id IN (".$rs['pid'].")";
$res=mysql_query($sql3) or die(mysql_error());
while ($row = mysql_fetch_array($res)){
$shipping_cost1= $shipping_cost1 + $row['shipping_outside'];
}
}
}
?>