我需要通过PHP查询选择日期类型字段。当我尝试将值修改为d-m-Y类型时,问题就表明了。我需要格式化这些数据,以便使用AJAX在图表中显示它。
如果我在Heidi中进行查询,它可以正常工作,但如果我通过PHP尝试它,我会收到以下错误:
MySQL查询:
SELECT COUNT(id) as total, DATE_FORMAT(login_at, '%d-%m-%Y') as datee FROM connections
WHERE login_at BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
GROUP BY datee;
PHP查询:
//查询从表中获取数据
$query = sprintf("SELECT COUNT(id) as total, login_at as datee
FROM connections
WHERE login_at BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND NOW()
GROUP BY datee;");
//执行查询
$result = $mysqli->query($query);
//遍历返回的数据
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
//现在打印数据
print json_encode($data);
错误:
<br />
<b>Warning</b>: sprintf(): Too few arguments in <b>C:\xampp\htdocs\chartjs-master\php-mysql-chartjs\data.php</b> on line <b>24</b><br />
<br />
<b>Warning</b>: mysqli::query(): Empty query in <b>C:\xampp\htdocs\chartjs-master\php-mysql-chartjs\data.php</b> on line <b>30</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\xampp\htdocs\chartjs-master\php-mysql-chartjs\data.php</b> on line <b>34</b><br />
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function close() on boolean in C:\xampp\htdocs\chartjs-master\php-mysql-chartjs\data.php:40
Stack trace:
#0 {main}
thrown in <b>C:\xampp\htdocs\chartjs-master\php-mysql-chartjs\data.php</b> on line <b>40</b><br />
我搜索并尝试了我在互联网上找到的每一个解决方案,但没有取得任何成功。