<?php header('Content-Type:application/json; charset=utf-8'); $row = array( array( 'id'=>5, 'title'=>'大王', 'writer'=>'太原市', 'source'=>'山西省', 'pubdata'=>1544779324, 'description'=>'大店区武城南路' ), array( 'id'=>6, 'title'=>'小王', 'writer'=>'大同市', 'source'=>'山西省', 'pubdata'=>1544779324, 'description'=>'小店区大同南路' ) ); $result = array(); $result['count'] = count($row); foreach($row as $v) { $result['data'][] = $v; } print_r($result); $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "dede"; $province = $_GET['province']; $city = $_GET['city']; $data=array(); // 创建连接 $link = mysql_connect($servername, $username, $password); mysql_select_db($dbname,$link); mysql_query("SET names UTF8"); $i=0; //判断城市为空 if(empty($city)){ $sql = "select id,title,writer,source,pubdate,description from dede_archives where source='{$province}'"; }else{ $sql = "select id,title,writer,source,pubdate,description from dede_archives where source='{$province}' and writer='{$city}'"; } $result = mysql_query($sql,$link); while($row = mysql_fetch_assoc($result)){ $i++; array_push($data,$row); } $arr=array("count"=>$i,"data"=>$data); $jsonObj_1 = json_encode($arr); echo $jsonObj_1; $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "dede"; $province = $_GET['province']; $city = $_GET['city']; $data=array(); // 创建连接 $link = mysql_connect($servername, $username, $password); mysql_select_db($dbname,$link); mysql_query("SET names UTF8"); $sql = "select id,title,writer,source,pubdate,description from dede_archives where source='{$province}'"; $result = mysql_query($sql,$link); $arr = $res = array(); while($row = mysql_fetch_assoc($result)){ $arr[] = $row; } $res['count'] = count($arr); foreach($arr as $v) { $res['data'][] = $v; } print_r($res); ?>