-3

I want to list all username from database if the id exists. That code works fine in database select * from users where id IN (1,2,3,4,5).

But in php it doesn't work. My php code is

$sql = "select * from users where id IN (1,2,3,4,5)"; 
$result = mysql_query($sql); 
if ( mysql_num_rows($result) > 0 ) { 
  $row = mysql_fetch_assoc( $result );
  echo $row["username"]; 
}

This php code only list one user name. I want to list all usernames. Sorry for my bad English. Thanks.

Naktibalda
  • 13,705
  • 5
  • 35
  • 51
Joe J.
  • 19
  • 1
  • 6

1 Answers1

1
<?php
$sql = "select * from users where id IN (1,2,3,4,5)"; 
$result = mysql_query($sql); 
if ( mysql_num_rows($result) > 0 ) {
    while($row = mysql_fetch_assoc( $result )) {
        echo $row["username"]."\n";
    } 
}

if you only fetch once, you will only get one row - obviously. so if you want all rows, you have to fetch as long as there are rows to fetch.

Franz Gleichmann
  • 3,420
  • 4
  • 20
  • 30