在我的项目中显示了有关图表的统计数据。其中一项数据是本周和上周网站的点击次数。两者目前都在自己的折线图中。那里的所有内容也都显示没有任何问题。但现在我想把本周的数据和上周的数据放到一个折线图中,以便快速比较本周和上周网站的访问量。因此,我尝试将两个查询的输出获取到一个 JSON 中,但总是收到此错误 ( json_encode() expects parameter 2 to be int, array given)。我认为这应该以不同的方式解决,但我还没有找到任何其他想法或方法。代码: header("Content-Type: application/json"); include "../../../includes/db.php"; $stmt = $connection->prepare("SELECT website_stats_homepage AS current_week FROM website_stats WHERE yearweek(DATE(website_stats_date), 1) = yearweek(curdate(), 1) ORDER BY website_stats_date"); //$stmt = $connection->prepare("SELECT website_stats_homepage FROM website_stats WHERE website_stats_date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND website_stats_date < curdate() - INTERVAL DAYOFWEEK(curdate())-2 DAY"); $stmt->execute(); $result = $stmt->get_result(); $stmt1 = $connection->prepare("SELECT website_stats_homepage AS last_week FROM website_stats WHERE website_stats_date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND website_stats_date < curdate() - INTERVAL DAYOFWEEK(curdate())-2 DAY"); $stmt1->execute(); $result1 = $stmt1->get_result(); $data = array(); foreach($result as $row) { $data[] = $row; } $data1 = array(); foreach($result1 as $row1) { $data1[] = $row1; } $stmt->close(); $stmt1->close(); echo json_encode($data, $data1);
1 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
将数据打包到关联数组中
echo json_encode(['current' => $data, 'last' => $data1]);
作为替代方案,您可以尝试使用子查询或联接来合并已通过查询的数据。
- 1 回答
- 0 关注
- 74 浏览
添加回答
举报
0/150
提交
取消