Two queries mysql in one object json

前端 未结 3 851
孤街浪徒
孤街浪徒 2021-01-03 17:11

I have two tables that I want to convert them to json like this:

[
   {
      \"date\":\"2013-07-20\",
      \"id\":\"123456\",
      \"year\":\"2013\",
             


        
3条回答
  •  粉色の甜心
    2021-01-03 17:21

    You can make it work by waiting to use the key people until the very end when you join the two arrays. Up until then, just load the data into $json and $json2.

    $json = array('date' => '2013', 'id' => '123456', 'year' => '2013');
    
    $result = mysql_query("SELECT * FROM data where id='123456'");
    $fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 
    
    $json = array();
    $json2 = array();
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $json[] = $row;
    }
    
    while ($row = mysql_fetch_assoc($fetch)){
        $row_temp["name"]=$row["name"];
        $row_temp["age"] = $row["age"];
        $row_temp["city"] = $row["city"];
        array_push($json2, $row_temp);
    }
    
    $json['people'] = $json2;
    
    echo Json_encode($json);
    

提交回复
热议问题