Two queries mysql in one object json

前端 未结 3 849
孤街浪徒
孤街浪徒 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);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-03 17:39

    I think you may try this

    $result = mysql_query("SELECT * FROM data where id='123456'");
    $fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 
    
    // I think, you'll get a single row, so no need to loop
    $json = mysql_fetch_array($result, MYSQL_ASSOC);
    
    $json2 = array();
    while ($row = mysql_fetch_assoc($fetch)){
        $json2[] = array( 
            'name' => $row["name"],
            'age' => $row["age"],
            'city' => $row["city"]
        );
    }
    $json['people'] = $json2;
    echo json_encode($json);
    

    Result of print_r($json) should be something like this

    Array
    (
        [date] => 2013-07-20
        [year] => 2013
        [id] => 123456
        [people] => Array
            (
                [0] => Array
                    (
                        [name] => First
                        [age] => 60
                        [city] => 1
                    )
    
                [1] => Array
                    (
                        [name] => second
                        [age] => 40
                        [city] => 2
                    )
    
            )
    
    )
    

    Result of echo json_encode($json) should be

    {
        "date" : "2013-07-20",
        "year":"2013",
        "id":"123456",
        "people":
        [
            {
                "name" : "First",
                "age" : "60",
                "city" : "1"
            },
            {
                "name" : "second",
                "age" : "40",
                "city" : "2"
            }
        ]
    }
    

    If you do echo json_encode(array($json)) then you will get your whole json wrapped in an array, something like this

    [
        {
            "date" : "2013-07-20",
            "year":"2013",
            "id":"123456",
            "people":
            [
                {
                    "name" : "First",
                    "age" : "60",
                    "city" : "1"
                },
                {
                    "name" : "second",
                    "age" : "40",
                    "city" : "2"
                }
            ]
        }
    ]
    
    0 讨论(0)
提交回复
热议问题