【问题标题】:How to get multiple tables and show them in JSON如何获取多个表并以 JSON 格式显示它们
【发布时间】:2022-01-24 13:11:04
【问题描述】:

我在 MySql 中有这些表

Countries -> countryId , countryName
Cities -> cityId , cityName , countryId
Neighborhoods -> neighborhoodId , neighborhoodName , cityId

输出应该是这样的

{
  "Countries": [
    {
      "countryId": "...",
      "countryName": "..."
    },
    {
      "countryId": "...",
      "countryName": "..."
    },
    {
      "countryId": "...",
      "countryName": "..."
    },
    {
      "countryId": "...",
      "countryName": "..."
    }
  ],
  "Cities": [
    {
      "cityId": "...",
      "cityName": "...",
      "countryId": "..."
    },
    {
      "cityId": "...",
      "cityName": "...",
      "countryId": "..."
    },
    {
      "cityId": "...",
      "cityName": "...",
      "countryId": "..."
    },
    {
      "cityId": "...",
      "cityName": "...",
      "countryId": "..."
    }
  ],
  "Neighborhoods": [
    {
      "neighborhoodId": "...",
      "neighborhoodName": "...",
      "cityId": "..."
    },
    {
      "neighborhoodId": "...",
      "neighborhoodName": "...",
      "cityId": "..."
    },
    {
      "neighborhoodId": "...",
      "neighborhoodName": "...",
      "cityId": "..."
    },
    {
      "neighborhoodId": "...",
      "neighborhoodName": "...",
      "cityId": "..."
    }
  ]
}

这段代码只给了我一张表的数据

<?php
$connection = new mysqli('...', '...', '...', '...');

if ($connection->connect_errno)
{
    printf("Failed to connect to database");
    exit();
}

$result = $connection->query("SELECT * FROM Countries");
$dbdata = array();
while ($row = $result->fetch_assoc())
{
    $dbdata[] = $row;
}
echo json_encode($dbdata);
?>

我是 PHP 新手,所以我希望有人告诉我如何像上面那样做。谢谢................................................ ..................................................... ....................................

【问题讨论】:

  • 请提供您尝试过的代码?
  • @Gnanavel 立即查看

标签: php mysql json


【解决方案1】:

试试这个...对于多个表,为每个表运行单独的查询。

<?php
// DB connection
$con = mysqli_connect("***","***","***","***");
if(!$con){
    echo mysqli_connect_error();
    exit;
}
$Countries = mysqli_query($con," SELECT * from Countries");
$Cities = mysqli_query($con," SELECT * from Cities ");
$Neighborhoods = mysqli_query($con," SELECT postNumber,adCode from Neighborhoods ");

$Countries1 = array();
$Cities1 = array();
$Neighborhoods1 = array();

if($Countries && $Cities && $Neighborhoods){
        while($Countries11 = mysqli_fetch_assoc($Countries)){
        $Countries1[] = $Countries11;
     }
     while($Neighborhoods11 = mysqli_fetch_assoc($Neighborhoods)){
        $Neighborhoods1[] = $Neighborhoods11;
     }
     while($Cities11 = mysqli_fetch_assoc($Cities)){
        $Cities1[] = $Cities11;
     }
}

$array = array();
$array["Countries1"] = $Countries1;
$array["Cities1"] = $Cities1;
$array["Neighborhoods1"] = $Neighborhoods1;

echo json_encode($array);
?>

您是否正在寻找类似的东西?

【讨论】:

  • 不,检查 JSON 更新
  • 我实际上认为它您正在寻找的,只需将“table1”更改为“Countries”,将“table2”更改为“Cities”,并将“table3”更改为” 成为上面$array 中的“社区”,这个答案应该与您想要的输出相匹配。
  • @JesseQ 输出和我想要的不一样,检查上面的JSON
  • @JesseQ 输出ibb.co/nBnTXXd
  • @SE19 立即尝试,已更新
【解决方案2】:

请检查下面的代码,我在代码本身中添加了 cmets 的解释。

<?php
    // connect your database   
    $conn = mysqli_connect('*****','('*****','('*****','('*****');
    
    // select the data from the 3 tables
    $country_res = mysqli_query($conn, 'select * from Countries');
    $city_res = mysqli_query($conn, 'select * from Cities');
    $nighbor_res = mysqli_query($conn, 'select * from Neighborhoods');

    // create 3 empty arrays to avoid notices, in case of 
    // empty results from database
    $countries = [];
    $cities = [];
    $neighborhoods = [];

    // fill countries array
    while($country = mysqli_fetch_assoc($country_res)) {
        $countries[] = $country;
    }
    // fill cities array
    while($city = mysqli_fetch_assoc($city_res)) {
        $cities[] = $city;
    }
    // fill neighborhoods array
    while($nighbor = mysqli_fetch_assoc($nighbor_res)) {
        $neighborhoods[] = $nighbor;
    }

    // print your results in the format you mentioned 
    //(you have to use HTML code in order for the browser to
    // display it the way you need, so I used <pre> tag.)
    echo '<pre>';
    echo json_encode(['Countries' => $countries, 
                      'Cities' => $cities, 
                      'Neighborhoods' => $neighborhoods]);
    echo '</pre>';
?>

【讨论】:

    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 2022-01-07
    • 2020-08-08
    • 1970-01-01
    • 2020-12-14
    • 2014-09-10
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多