我可以从一个页面抛出多个json吗?

gfttwv5a  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(265)

我想从这个页面抛出多个json,以便获取一个json文件,用java编程显示我的移动应用程序的输出。下面是我的代码,它显示了表“news”中正确的json,但是,我想抛出数据库中其他可用对象的json。有可能吗?

$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

    mysqli_query($dblink, 'SET NAMES utf8');

    //Check connection was successful
      if ($dblink->connect_errno) {
         printf("Failed to connect to database");
         exit();
      } 

      $result = $dblink->query("SELECT * FROM news ORDER BY id DESC");

      $dbdata = array();

      while ( $row = $result->fetch_assoc())  {
        $dbdata[]=$row;
      }

     echo json_encode($dbdata);

    ?>
vawmfj5a

vawmfj5a1#

这取决于你如何组合它们,它可以简单到:
合并:

$dbdata = array_merge($dbdata1, $dbdata2);
echo json_encode(dbdata);

不同的键:

$dbdata = array(
'table_1' => $dbdata1,
'table_2' => $dbdata2
);
echo json_encode($dbdata);

mysql加入:

SELECT n.*, m.* FROM news n LEFT JOIN news_meta m ON m.news_id = n.news_id ORDER BY n.id DESC;

/* $dbdata will then contain your two tables worth of data: */
echo json_encode($dbdata);

相关问题