【发布时间】:2015-04-23 19:20:14
【问题描述】:
我有一个 MySQL 表,其中包含以 CURRENT_TIMESTAMP 为日期的数据。我正在尝试使用 PHP 查询过去 30 天的数据,然后将其转换为 JSON 对象,然后再将其发送回我的 UI。目前我只得到一个空的 JSON 对象。这是我当前的 ajax 代码。
function dailyReport(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState===4 && xmlhttp.status===200){
document.querySelector("#reportsOutput").innerHTML="";
document.querySelector("#reportsOutput").innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("GET","php/dailyReport.php",true);
xmlhttp.send();
}
这是我当前的 PHP 代码
$connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
$query = "SELECT * FROM items WHERE timestamp=current_timestamp";
$result = mysqli_query($connection, $query);
$rows=array();
while($r=mysqli_fetch_assoc($result)){
$rows[]=$r;
}
$response=json_encode($rows);
echo($response);
【问题讨论】: