Two queries mysql in one object json

前端 未结 3 850
孤街浪徒
孤街浪徒 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:38

    You were very close, but you want the People array to be a direct value of the outer array and you've wrapped it in an extra array.

    Also, please note that the MySQL library you are using is deprecated. That means it will be removed from PHP in a future release. You should replace calls from the MySQL_* family of functions with either mysqli or pdo

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

提交回复
热议问题