1

I created json format from many table by using query(Union).But I get only one json result instead of three results.The result is

["Used","Huawei","Samsung","Mandalay"]

So,I want to insert name in front of "Used" like that ["Condition":"Used"]. How can I do that?

Here is my code

<?php
    $result=mysql_query(" SELECT ttd.name 
                      FROM   `taxonomy_term_data` AS ttd
                      INNER JOIN `field_data_field_ph_condition` AS fdc
                      ON   fdc.field_ph_condition_tid=ttd.tid
                      INNER JOIN `node` AS N
                      ON n.nid=fdc.entity_id 
                      UNION
                      SELECT ttd.name 
                      FROM   `taxonomy_term_data` AS ttd
                      INNER JOIN `field_data_field_brand` AS fb
                      ON   fb.field_brand_tid=ttd.tid
                      INNER JOIN `node` AS N
                      ON n.nid=fb.entity_id 
                      UNION
                      SELECT ttd.name 
                      FROM   `taxonomy_term_data` AS ttd
                      INNER JOIN `field_data_field_city` AS fb
                      ON   fb.field_city_tid=ttd.tid
                      INNER JOIN `node` AS N
                      ON n.nid=fb.entity_id 
                      ") or die(mysql_error());

 while ($row = mysql_fetch_array( $result, MYSQL_ASSOC)) {
 $json_response[] = $row['name'];
}
  echo json_encode($json_response); 
?>
  • `json_encode(['Condition' => $json_response[0]])` In this case, you won't have to use the whole UNION btw - the first subquery should suffice. – raina77ow Sep 22 '15 at 18:34
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 22 '15 at 18:52

0 Answers0