开发者

Output the data from MySQL into an 2 dimensional Array

开发者 https://www.devze.com 2023-04-11 02:03 出处:网络
I have record rows in MySQL as id=0name=tomgrade=A id=1name=jeff grade=B id=2name=lisa grade=B ....... etc

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

精彩评论

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