一个对象json中的两个查询mysql

Posted

技术标签:

【中文标题】一个对象json中的两个查询mysql【英文标题】:Two queries mysql in one object json 【发布时间】:2013-07-28 12:07:06 【问题描述】:

我有两个表,我想像这样将它们转换为 json:

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

但是我的代码的结果是这样的:

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

代码为数组“people”创建了一个新对象,我希望它在同一个对象中

$result = mysql_query("SELECT * FROM data where id='123456'");
$fetch = mysql_query("SELECT name,age,city FROM people where id='123456'"); 

$json = array();
$json2['people'] = 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['people'],$row_temp);
   

    array_push($json, $json2);

echo Json_encode($json);

如何使数组与表“数据”在同一个对象中?

非常感谢

【问题讨论】:

【参考方案1】:

我想你可以试试这个

$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);

print_r($json) 的结果应该是这样的

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
                )

        )

)

echo json_encode($json) 的结果应该是


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

如果您使用echo json_encode(array($json)),那么您会将整个json 包裹在一个数组中,类似于这样

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

【讨论】:

感谢您的回答。我尝试了三个示例都响应并返回了相同的代码。 "0": "date":"2013-07-20", "id":"123456", "year":"2013" , "people":[....] 我想要像这样 [ "date":"2013-07-20", "id":"123456", "year":"2013", "people":[......] ] 你知道吗纠正它?谢谢! 也许,您应该发布您的查询结果。 你是指数据库中查询的结果还是json编码的结果? 两个查询的结果,不是 json。 SELECT * FROM data where id='123456' date year id 2013-07-20 2013 123456 SELECT name,age,city FROM people where id='123456' name age city id First 60 1 123456 second 40 2 123456 city 36 1 123456 格式抱歉【参考方案2】:

您可以通过等待使用密钥 people 直到您加入两个数组的最后一刻来使其工作。在那之前,只需将数据加载到$json$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);

【讨论】:

【参考方案3】:

您非常接近,但您希望 People 数组是外部数组的直接值,并且您已将其包装在一个额外的数组中。

另外,请注意您使用的 MySQL 库已弃用。这意味着它将在未来的版本中从 php 中删除。您应该使用 mysqli 或 pdo 替换来自 MySQL_* 系列函数的调用

$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);

【讨论】:

以上是关于一个对象json中的两个查询mysql的主要内容,如果未能解决你的问题,请参考以下文章

查询以连接 MySQL 中的两个 json 数组?

Mysql - 直接查询存储的Json字符串中的数据

Mysql - 直接查询存储的Json字符串中的数据

Mysql - 直接查询存储的Json字符串中的数据

使用 JOOQ 查询 mysql 中的 json 字段

如何使用 AND 查询 MYSQL 中的 JSON 数组