简体   繁体   English

从表MYSQL获取所有ID

[英]Get all id's from table MYSQL

I'd like to echo all ID's from the user table but I can't get it to work. 我想从用户表中回显所有ID,但无法正常工作。 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>";
            }
        }

Remove count(*) AS n from your query. 从查询中删除count(*) AS n This will cause MySQL to return only 1 record. 这将导致MySQL仅返回1条记录。

You also have some errors in your first lines: 您在第一行中还存在一些错误:

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

You forgot to define $active_ids and add [] . 您忘记定义$active_ids并添加[]

You need to make $active_ids a CSL. 您需要将$active_ids CSL。 Currently you overwrite the id(s) on every iteration so you only getting the last one. 目前,您在每次迭代中都覆盖了ID,因此只能获取最后一个ID。 You also need it separated by commas so use implode . 您还需要将其用逗号分隔,因此请使用implode Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM