带MySQL的PHP​​仅为Table的第一行返回正确的结果

I've recently gotten a PHP script i've written to return data back to my android java code, Unfortunately the script will only return data when I make a call to the first row of my table.

My Table is formatted as such:

1 Jared Jones
2 Karla Cross
3 Jasmine Smith
4 Vince Stevens

The numbers correspond to a UserId and the other 2 attributes are FName and LName.

My php script to execute the MySQL script is as follows:

    <?php 
mysql_connect("my database credentials go here");
mysql_select_db("a2275354_gtchose");
$sql=mysql_query("select FName,LName from Test where UserId ='".$_REQUEST['userId']."' ");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

I'm unsure why the only time i receive a result is when i pass 1 as the userId to the query. Any ideas?

Thanks

try removing the quotes around userId. Its a numeric field

$sql=mysql_query("select FName,LName from Test where UserId =".$_REQUEST['userId']);

I'm not sure if this would work but you can try this. Adding brackets to your field fname and lname

$sql=mysql_query("Select (Fname,LName) from Test where UserId='".$_REQUEST['userID']."' ");

You didn't declare your array before while loop...so put:

$output = array();

before while, so it should be:

$output = array();
$sql=mysql_query(...);
while($row=mysql_fetch_assoc($sql))
  $output[]=$row;

Also, as others pointed out, this is vurnerable to sql injection, so use mysql_real_escape_string.

Also, if your id is numeric, you should really remove that single quote around it...though it doesn't matter it should return results anyway...