php - 从表 MYSQL 中获取所有 id

标签 php mysql mysqli

我想回显用户表中的所有 ID,但我无法让它工作。我在下面尝试了这个并搜索了一些堆栈,但我找不到正确的答案。

        $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>";
            }
        }

最佳答案

从您的查询中删除 count(*) AS n。这将导致 MySQL 只返回 1 条记录。

你的第一行也有一些错误:

$active_ids = array();
while( $array = $db->assoc( $query ) ) {
    $active_ids[] = $array['id'];
}

您忘记定义 $active_ids 并添加 []

关于php - 从表 MYSQL 中获取所有 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446039/

相关文章:

php - 按类别对结果进行分组,然后将结果分成几天?

如果不包含 $parallel litem,php 变量将不会保存到数据库中

php - PHP 正则表达式模式中需要额外的反斜杠

php - aws + centos 7 + 内置 nginx ngx_pagespeed + php-fpm = 找不到文件

php - android + mysql + 登录

php - 在 header (H1) 标记中显示数组值

php - 如何通过mysql查询获取表结构及其数据?

php - 错误 : "Object reference not set to an instance of an object" when calling SOAP cilent in PHP

php - Laravel 5 删除尾部斜线时的不良行为

python - 获取 django 的最新条目