I have record rows in MySQL as
id=0 name=tom grade=A
id=1 name=jeff grade=B
id=2 name=lisa grade=B
....... etc
Now I want to output that do a 2 dimensional array such as
c开发者_开发技巧ontent{
{id=0
name=tom
grade=A
}
{id=1
name=jeff
grade=B}
{id=2
name=lisa
grade=B}}
$query="SELECT * FROM `user`";
$result=mysql_query($query);
$grades=array();
while($row=mysql_fetch_assoc($result)) {
...........
}
What I should put into the while loop?
mysql_fetch_assoc returns a associative array with the col name as the key and the value as the value. You have almost got everything you need to put this in another array how you want. Just put the following code in the loop to append the assoc arrays from each row into the $grades array:
$grades[] = $row;
Then you can access the values of the grades array like so:
$grades[1]['grade'] //returns the grade of row 1
精彩评论