PHP MySQL的数组检索每个记录 [英] PHP array mysql retrieve each record

查看:176
本文介绍了PHP MySQL的数组检索每个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 数据库(书表)

    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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆