dongxun2903 2014-03-08 13:03
浏览 100
已采纳

如何用sql获取行数?

How can I get the numbers of the lines where userid=8. I have a table with cloumns like ID and userid. So I am trying to put in php array the numbers of the lines that userid=8.

exmple:

  • I have lines 1-10, and userid=8 at lines: 1,4,7,10.
  • $counter[1] = 1
  • $counter[2] = 4
  • $counter[3] = 7
  • $counter[4] = 10

Table image

I tried to use this answer: Get all mysql selected rows into an array but its didnt work.

This is how I am getting the lines where userid=8 , but how can I put them into array?

$midSelect[i]="SELECT * FROM times WHERE userid=8;
$midResult[i]=mysqli_query($con,$midSelect[i]); 

while($row = mysqli_fetch_array($midResult[i])) {
$mid[i]=$row['id'];
}
  • 写回答

2条回答 默认 最新

  • douzhoubing2805 2014-03-08 13:19
    关注

    I think you are just one step away from do it

    $sql ="SELECT id FROM times WHERE userid=8";
    $query =mysqli_query($con,$sql); 
    
    $mid = array();
    while($row = mysqli_fetch_array($query)) {
        $mid[] = $row['id'];
    }
    
    print_r($mid);
    

    Your required values are now stord in your array $mid

    Upon op request i add some code

    Now if you need to echo values you can loop in the array and print while iterating

    foreach($mid as $val)
    {
        echo $val . '<br />';
    }
    //print 
    // 1
    // 4 
    // 5
    // 8
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?