PHP MySQL的数组检索每个记录 [英] PHP array mysql retrieve each record
问题描述
-
数据库(书表)
serialID           
0001 10.00
  0001             
30.00
0002 15.00
  0003(A)           9.00
  0004(B)      &NBSP ;    5.00
0005 3.00
Database (book table)
serialID price
0001 10.00
0001 30.00
0002 15.00
0003(A) 9.00
0004(B) 5.00
0005 3.00
code
$serialID = array("0001","0002","0003","0004","0005");
//DB Connection
for($i = 0; $i < count($serialID); $i++)
{
$q = "select serial from book where serial like \"$serialID[$i]\" limit 1";
$r = mysqli_query($dbc, $q);
}
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)
{
echo $row['serial'].'<br />';
}
从我的code,我只能只能得到最后的serialID。如何检索每个 serialID 在 $行
?
推荐答案
您可以使用它代替:
$q = "select serial from book where serial like '%"
. implode($serialID, "%' OR serial LIKE '%") . "%'";
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)
echo $row['serial'].'<br />';
在你的为
循环您 SELECT
所有的行,但只有最后一个实例被保存在一个变量(在为
循环的最后一次迭代),所以你只获取它在,而
循环。
In your for
loop you SELECT
all of the rows, but only the last instance is saved in a variable ( in the last iteration of the for
loop), so you fetch only it in the while
loop.
的 P.S。你也可以工作了把你的,而
循环中的为
循环的结束,而是code时上部还没有那么繁琐。的
P.S. You could also work it out by putting your while
loop in the end of the for
loop, but the code above is just not that cumbersome.
这篇关于PHP MySQL的数组检索每个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!