返回并解析mysql_fetch_assoc的结果

I'm having trouble getting values out of my array returned from a MySQL query.

The results that are being returned are a table with these columns:

|team_id|name|pos|available|

There are multiple rows in the result. I need to go through each row and extract name and pos into their respective variables.

Here is my code:

$query = sprintf("SELECT * FROM `player_user` WHERE team_id = '$teamID[0]'");
    $answer = mysql_query($query);
    if ($answer === FALSE)
        die(mysql_error());

    while($row = mysql_fetch_assoc($answer)) 
    {
        $pname = $row['name'];
        $pos = $row['pos'];


     ... do something with $pname and $pos 
    }

The example above should work, as long as the mysql query will return data. You should verify this using var_dump($row); inside the loop.

Although you should use the mysqli extension or PDO to access mysql databases. mysql_* functions as you currently use are deprecated and will once being dropped from PHP

You should use mysqli_fetch_array instead of mysql_fetch_assoc. That should return the results as you want.

http://php.net/manual/en/mysqli-result.fetch-array.php