简体   繁体   中英

how to format mysql results to json php

mysql table

ID >> Name >> Salary

$row_set << database table information.

my problem is when i use

json_encode($row_set);

the output will be something like this:

[{"0":"1","ID":"1","1":"x","Name":"x","2":"12345","Salary":"12345"}]

i want the results to be something like this

[{"ID":"1","Name":"x","Salary":"12345"}]

how to do that ?

EDIT :: FULL CODE

$result = mysql_query("SELECT * FROM emp");

while($row = mysql_fetch_array($result))
  {
     $row_set[] = $row;
  }
echo json_encode($row_set);

I presume you are using mysql_fetch_array to get the row at the moment.

Try mysql_fetch_array($resource, MYSQL_ASSOC) (note the 2nd parameter!)

or mysql_fetch_assoc() .

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