简体   繁体   English

将MySQL中的数据输出到二维数组中

[英]Output the data from MySQL into an 2 dimensional Array

I have record rows in MySQL as 我在MySQL中记录了行

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? 我应该在while循环中放入什么?

mysql_fetch_assoc returns a associative array with the col name as the key and the value as the value. mysql_fetch_assoc返回一个关联数组,其中col名称为键,值为值。 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: 只需将以下代码放入循环中,即可将每行的assoc数组追加到$ grades数组中:

$grades[] = $row;

Then you can access the values of the grades array like so: 然后,您可以像这样访问grades数组的值:

$grades[1]['grade'] //returns the grade of row 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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