Rackover Rackover - 4 months ago 8
SQL Question

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 ?

Answer

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++;
}