gpt4 book ai didi

php - 如何从mysql表中选择数据

转载 作者:行者123 更新时间:2023-11-29 22:30:38 25 4
gpt4 key购买 nike

我有一个 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);

最佳答案

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

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

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

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

我建议使用类似Sequel Pro的东西在将查询放入 PHP 之前对其进行测试,这样可以更轻松地查找此类错误。

希望这有帮助!

关于php - 如何从mysql表中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com