我有以下查询:
SELECT coalesce(COUNT('view_id'),0) level1count,y,m
from
(select 1 as m,2016 as y
union
select 2 as m,2016 as y
union
select 3 as m,2016 as y
union
select 4 as m,2016 as y
union
select 5 as m,2016 as y
union
select 6 as m,2016 as y
union
select 7 as m,2016 as y
union
select 8 as m,2016 as y
union
select 9 as m,2016 as y
union
select 10 as m,2016 as y
union
select 11 as m,2016 as y
union
select 12 as m ,2016 as y
) months
left join page_views on(months.m = month(page_views.view_date) and months.y = year(page_views.view_date))
group by months.m, year(page_views.view_date)
结果如下:
level1count Year Month
-----------------------------------------
1 2016 1
1 2016 2
1 2016 3
1 2016 4
34 2016 5
1392 2016 6
49 2016 7
1095 2016 8
1173 2016 9
859 2016 10
605 2016 11
100 2016 12
我需要像这样显示level1count:
1,1,1,1,34,1392,49,1095,1173,859,605,100
使用PHP - 我是新手并在其中开发网站。我想也许它需要放在一个数组然后以某种方式回应?
很抱歉,如果这是一个愚蠢的问题,但就像我说的那样 - 对PHP非常新,并慢慢掌握它。
非常感谢
JS
答案 0 :(得分:1)
这样可以解决问题:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$db_connection = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db_connection->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "your query here";
$result = $db_connection->query($sql);
$arrLevel1 = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
array_push($arrLevel1, $row['level1count'];
} else {
echo "0 results";
}
$comma_separated = implode(",", $arrLevel1);
echo $comma_separated;
$conn->close();
?>
*请记住,查询只是让事情变得简单的一个例子。它不安全,允许sql注入。