TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Merging several MySQL results into a single JSON encode (PHP)

Here is code as exists now:

while($row=mysql_fetch_assoc($count_query_result))
    $output[]=$row;
while($row=mysql_fetch_assoc($average_query_result))
    $output2[]=$row;
while($row=mysql_fetch_assoc($items_query_result))
    $output3[]=$row;

print(json_encode(array($output,$output2,$output3)));
mysql_close();

My question:

How do I take a single column from each of the three query results, and make a JSON array out of it, like so:

[{ 'att1' : 'data'}, { 'att2' : 'data'}, { 'att3' : 'data'}]

ASSUMING:

Therefore, encoding only one variable, not 3.

Upvotes: 1

Views: 738

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Well I answered my own issue. I had to get to the very root of the problem. The MySQL queries. I have joined them all so now there is just one. This creates a single JSON array for what I need. I believe there is something to be said about just doing it ... right .. the first time.

Upvotes: 1

Jim
Jim

Reputation: 1315

$result = array('att1' => $row['data'],
                'att2' => $row['data']

echo json_encode($result)

where $row['data'] is the information that you want returned from each of your queries

Upvotes: 0

Related Questions