dpmwy80068 2011-04-14 12:22
浏览 52
已采纳

php mysql for循环

in my query i want only from id 1-3 to appear in the result, not 4-6. is there any other way to do it?

beautician database table:
id_beautician   name
1               a
2               b
3               c
4               d
5               e
6               f

i tried to put the code below in mysql query

for($i=4; $i<=6; $i++) :
    $q.$i = $db->query("SELECT * FROM beautician WHERE id_beautician='$i'");
    $r.$i = $q.$i->fetch_assoc();
    //echo $i;
endfor;

but it gives me an error:

Object of class mysqli_result could not be converted to string

  • 写回答

2条回答 默认 最新

  • dsljpwi494719 2011-04-14 12:33
    关注

    If ids are not in sequence, you can also use as below.

    $query = $db->query("SELECT * FROM beautician WHERE id_beautician in (1,2,3)");
    while ( $row = $query->fetch_assoc() ) 
    {
        echo $row['id'] . ' = ' . $row['name'] . '<br/>';
    }
    

    using IN in query sometimes slow down the execution, but can be used efficiently for small-size database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统