简体   繁体   中英

SQL SELECT * returns only one row

I'm designing a website and I wrote some webpage that display the list of users. I used to do a

$query = SELECT * FROM `table_users` WHERE `id`='.$id.'

and then increment the ID with a "while" so I can grab all the users. But it's too slow now, and it glitches when there is a gap between IDs.

So I tried something like

$query = SELECT `name` FROM `tbl_user`ORDER BY `id`

and displaying the userlist with a

while ($i < sizeof(mysql_fetch_array(mysql_query($query)))){
<code to display an user>
$i++
}

But the mysql_fetch_array only returnes one user, the first one (the one with the littliest ID). I want it to return all users in an array. How do I do ?

Try it like this:

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
     //$row is the row which was just gathered
}

And please, use PDO or MySQLi instead of deprecated mysql.

Try This

$query = "SELECT `name` FROM `tbl_user` ORDER BY `id`";
$user_query = mysql_query($query);

$i=1;
while ($row = mysql_fetch_array($user_query)){
  echo $i." : ".$row['name']."<br>";
  $i++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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