dongxing2030 2015-10-30 22:55
浏览 157
已采纳

从表MYSQL获取所有id

I'd like to echo all ID's from the user table but I can't get it to work. I tried this below and searched some stacks but I can't find the right answer.

        $query = $db->query( "SELECT * FROM users" );
        $num   = $db->num( $query );
        while( $array = $db->assoc( $query ) ) {
            $active_ids = $array['id'];
        }

        $query = "SELECT u.username, u.habbo, count(*) AS n 
        FROM users u, timetable tt 
        WHERE u.id=tt.dj and u.id IN (".$active_ids.")
        GROUP BY tt.dj";

        $result = $mysqli->query($query);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()){
                echo "<tr id='uren-table-row'>";
                echo "<td width='60px'><div style='height:50px;padding-top:25px;overflow:hidden;float:left;margin-top:-25px;'><img style='margin-top:-28px;float:left;' src='http://habmoon.org/moonstream/dj?habbie={$row['habbo']}&amp;head_direction=3&amp;direction=2&amp;size=b&amp;hb=img&amp;gesture=sml'></div></td>";
                echo "<td width='200px' style='padding:0 15px;'>", $row['username'] ,"</td>";
                echo "<td style='padding:0 15px;'>Heeft <b>", $row['n'] ,"</b> Uur gedraait deze week</td>";
                echo "</tr>";
            }
        }
  • 写回答

2条回答 默认 最新

  • drpfu51608120170 2015-10-30 23:11
    关注

    You need to make $active_ids a CSL. Currently you overwrite the id(s) on every iteration so you only getting the last one. You also need it separated by commas so use implode. Try:

    while( $array = $db->assoc( $query ) ) {
                $active_ids[] = (int)$array['id'];
     }
     $active_ids = implode(',', $active_ids);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀