开发者

Output the data from MySQL into an 2 dimensional Array

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜