如何在php中将多个表结果添加到单个json中

时间:2016-06-23 10:10:18

标签: php mysql json

two tables Structures:>> 这个php文件包含两个表(college1和产品)所以我需要将这些表结果输入到单个json.that应该包含这些所有数据。感谢提前< / p>

<?php

             function college1()
            {
                 require_once __DIR__ . '/DB_Connect.php';
                 $db = new DB_CONNECT();

                $retArr = array();

                $latest ='';
                $datetime=mysql_query("SELECT MAX(time) AS latest from college1");
                while ($row = mysql_fetch_assoc($datetime)) 
                {
                    $latest = $row['latest'];           
                }



                if(isset($_REQUEST['lasttime']))
             { 
                $dates=$_REQUEST['lasttime'];

                $query=mysql_query("SELECT * from college1  WHERE time >'{$dates}'");

                while ($rows = mysql_fetch_assoc($query)) 
                {

                    $retArr['college1']['data'][] = $rows;



                }

                $retArr['college1']['last_date'] = $latest;
                $retArr['json']=$retArr['college1'];


                $retArr = json_encode($retArr);
                echo($retArr);
            }
            else 
            {
                    $query = mysql_query("SELECT * FROM college1");


                            while($row = mysql_fetch_assoc($query))
                    {

                        $retArr['college1']['data'][] = $row;

                    }
                    $retArr ['college1']['last_date'][] = $latest ;


                    $retArr = json_encode($retArr);
                    echo($retArr);

            }



             function product1()
            {
                 require_once __DIR__ . '/DB_Connect.php';
                 $db = new DB_CONNECT();

                $pr_result = array();
                $latest ='';
                $datetime=mysql_query("SELECT MAX(time) AS latest from products");

                while ($row = mysql_fetch_assoc($datetime)) 
                {
                    $latest = $row['latest'];           
                }



                if(isset($_REQUEST['lasttime']))
             { 
                $dates=$_REQUEST['lasttime'];

                $query=mysql_query("SELECT * from college1  WHERE time >'{$dates}'");

                while ($rows = mysql_fetch_assoc($query)) 
                {

                    $pr_result['product1']['data'][] = $rows;



                }

                $pr_result['product1']['last_date'] = $latest;


                $pr_result = json_encode($pr_result);
                echo($pr_result);
            }
            else 
            {
                    $query = mysql_query("SELECT * FROM products");


                            while($row = mysql_fetch_assoc($query))
                    {

                        $pr_result['college1']['data'][] = $row;

                    }
                    $pr_result = json_encode($pr_result);
                        echo($pr_result);
            }           
?>

1 个答案:

答案 0 :(得分:0)

您可以编写连接查询以下是您可以用来生成json并在andorid或其他中使用它的示例网站

http://androidtutorialforbeginners.com/php-webservice-json-generator/