我想基于他们的sdk来检索一些产品的ID ...但是如果有2个或多个产品,它只返回我的第一个ID ......
任何人都可以帮助我吗?
非常感谢
这是我用的代码......(我是php的新手)
<?
foreach(array_keys($nume_individual) as $n){
$sql = mysql_query("SELECT * FROM products WHERE cod_produs = '$cod_produs_individual[$n]' LIMIT 1");
while($row = mysql_fetch_assoc($sql)){
$id_s[$n]=$row["id"];
}
echo (
"<tr>
<td>".$id_s[$n]."</td> /* this is not working well (running just once) */
<td>".$nume_individual[$n]."</td>
<td>".$cod_produs_individual[$n]."</td>
<td>".$numar_individual[$n]."</td>
<td>".$bucati_individual[$n]."</td>
<td>".$pret_individual[$n]."</td>
</tr>
");
};?>
答案 0 :(得分:0)
因为你已经设置了限制1,所以它只返回一行
$sql = mysql_query("SELECT *
FROM products
WHERE cod_produs = '$cod_produs_individual[$n]'
LIMIT 1 // this is the reason you getting only one row
");
第二个你使用while循环之外的echo使用while loopp
答案 1 :(得分:0)
将echo
放入while循环中,然后从查询中删除limit 1
。
如果查询中有limit 1
,则限制结果只能从数据库返回1行。此外,如果在while循环之外有回显,则只会从数据库中打印最后加载的行。
您还可以从某些阵列打印$ n键处的值。我不知道$ n是什么,但您可能想尝试使用行的ID,因为您使用的是主foreach索引。
foreach(array_keys($nume_individual) as $n){
$sql = mysql_query("SELECT * FROM products WHERE cod_produs = '$cod_produs_individual[$n]'");
while($row = mysql_fetch_assoc($sql)){
$id_s[$n]=$row["id"];
echo (
"<tr>
<td>".$row["id"]."</td>
<td>".$nume_individual[$row["id"]]."</td>
<td>".$cod_produs_individual[$row["id"]]."</td>
<td>".$numar_individual[$row["id"]]."</td>
<td>".$bucati_individual[$row["id"]]."</td>
<td>".$pret_individual[$row["id"]]."</td>
</tr>"
);
}
}
答案 2 :(得分:0)
我设法解决了这个问题。这是代码,也许对某人有用:)
<?
$cod_produs_individual2 = array_pop($cod_produs_individual) ;
$csv =implode(',', $cod_produs_individual);
$csv2 = "'" . join("', '", $cod_produs_individual) . "'";
$csv3= $string = rtrim($csv2, ',');
$query2 = sprintf("SELECT *
FROM products
WHERE cod_produs IN (%s)",
$csv3);
$results2 = mysql_query($query2);
$randuri = mysql_num_rows($results2);
$a = array();
if ($randuri >0) { while($row = mysql_fetch_array($results2)){
array_push ($a, $row["id"]);
}
}
print_r ($a);
foreach(array_keys($nume_individual) as $n){
echo (
"<tr>
<td>".$a[$n]."</td>
<td>".$nume_individual[$n]."</td>
<td>".$cod_produs_individual[$n]."</td>
<td>".$numar_individual[$n]."</td>
<td>".$bucati_individual[$n]."</td>
<td>".$pret_individual[$n]."</td>
</tr>
");
};?>