简体   繁体   中英

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

content{
{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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM