I'm facing a problem with reading an associative array from an SQL query on my PostgreSQL database.
So here's my database:
ID | NAME | ....
1 | CARS |
2 | BIKES|
3 | TRAINS |
Now I have a PHP script that I want to grab some data from that query and encode it into JSON
for a mobile device.
Here is the script:
$res = pg_query("SELECT * FROM projects");
/* FETCHES THE RESULT OF THE SQL QUERY WHICH GETS THE NAME OF EACH PROJECT */
while($row = pg_fetch_assoc($res))
{
$output[]=$row['name'];
print (json_encode($output));
} /* CONVERTED ON MOBILE PLATFORM */
pg_close();
Now the result I'm getting is shown copy and pasted from the output of the file (below):
["Cars"]["Cars","Bikes"]["Cars","Bikes","Trains"]
I can clearly see that my algorithm is indexing the records is a sort of 1, 1,2, 1,2,3 kind of way. Has anyone got any advice on how to solve this and to get this output:
["Cars"]["Bikes"]["Trains"]
$output[]
to$output
?[]
! I believe that is what I want but are they separate values or an individual value? I need them to be separate as I'm populating a spinner on an android device!