问题
Thanks to all you gurus out there.
I am trying to make a previous and next button on a profile page. If I am looking at the profile for Arnsdorf, I want a previous button to link to Antonio and a next button to link to Baldeviso.
IDs aren't sequential as you can see from the output which is ordered by lname:
ID: 22 Name: Airaghi
ID: 36 Name: Antonio
ID: 27 Name: Arnsdorf
ID: 13 Name: Baldeviso
ID: 46 Name: Barnes
Here's my code:
    $sql = "SELECT id,lname FROM profiles ORDER BY lname";
    $query = mysql_query($sql);
        if (!$query) {
            die('Invalid query: ' . mysql_error());
        }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        printf ("ID: %s  Name: %s", $row[0], $row["lname"]);
          echo "<br>";
    }
mysql_free_result($query);
But I can't get it into an array to sort it. 
I was hoping to create a 3 column multi-dimensional array with a data set that looks roughly like this:
row,id,lname
1,22,Airaghi
2,36,Antonio
3,27,Arnsdorf
4,13,Baldeviso
5,46,Barnes
I could then figure out how to get the number of the row for say Arnsdorf. Then get the row number for +1 and -1, so that I could get their respective ids for use in my hyperlink.
I have looked for hours and I can't find any code examples that will dump the mysql data into a numbered multi-dimensional array permanent enough to do this sort on. Thanks for your help.
回答1:
You can assing each row to array, and then echo data from that array. Here is example:
$persons = array();
while($row = mysql_fetch_assoc($query)) {
    $persons[] = $row;
}
// To access ID of user in third row
echo $persons[2]['id'];
// Name of that person
echo $persons[2]['lname'];
    回答2:
It's actually pretty simple:
while($row = mysql_fetch_array($query)){
    $profiles[] = $row;
}
Now you will have an array like this:
array(
    0 => array('id' => 22, 'lname' => 'Airaghi'),
    1 => array('id' => 36, 'lname' => 'Antonio'),
    etc...
)
Then you can just access it from $profiles[$index]['id'] or $profiles[$index]['lname']
来源:https://stackoverflow.com/questions/8611207/dump-mysql-fetch-array-results-into-a-multidimensional-array