在我的項目中顯示了有關(guān)圖表的統(tǒng)計數(shù)據(jù)。其中一項數(shù)據(jù)是本周和上周網(wǎng)站的點(diǎn)擊次數(shù)。兩者目前都在自己的折線圖中。那里的所有內(nèi)容也都顯示沒有任何問題。但現(xiàn)在我想把本周的數(shù)據(jù)和上周的數(shù)據(jù)放到一個折線圖中,以便快速比較本周和上周網(wǎng)站的訪問量。因此,我嘗試將兩個查詢的輸出獲取到一個 JSON 中,但總是收到此錯誤 ( json_encode() expects parameter 2 to be int, array given)。我認(rèn)為這應(yīng)該以不同的方式解決,但我還沒有找到任何其他想法或方法。代碼: 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貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個贊
將數(shù)據(jù)打包到關(guān)聯(lián)數(shù)組中
echo json_encode(['current' => $data, 'last' => $data1]);
作為替代方案,您可以嘗試使用子查詢或聯(lián)接來合并已通過查詢的數(shù)據(jù)。
- 1 回答
- 0 關(guān)注
- 105 瀏覽
添加回答
舉報
0/150
提交
取消