【问题标题】:How to select data from a mysql table [closed]如何从mysql表中选择数据[关闭]
【发布时间】: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);

【问题讨论】:

    标签: php mysql ajax json


    【解决方案1】:

    看起来 SQL 查询可能是这里的问题。

    $query = "SELECT * FROM items WHERE timestamp=current_timestamp";
    

    是说,“选择所有时间戳为现在的项目。”但是,我认为您的意思是按照这些思路做一些事情,您选择一个大于当前时间戳的日期 - 30 天:

    $query = "SELECT * FROM items WHERE timestamp > DATE_SUB(current_timestamp,INTERVAL 30 DAY)";
    

    我建议在将查询放入 PHP 之前使用Sequel Pro 之类的东西来测试它们,这样可以更轻松地找出这样的错误。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      看看这个答案 - 你需要修改你的查询。

      MySQL Select last month data by current_timestamp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2011-05-17
        相关资源
        最近更新 更多