0

我基本上只是想从一个表中选择一个数组......它不能正常工作。

我有以下查询

$graph = mysql_query("SELECT MONTHNAME(dateadded) MONTH, COUNT(*) COUNT
FROM products
WHERE ((YEAR(dateadded)=2012) && (site_url = '$_GET[site_url_graph]'))
GROUP BY MONTH(dateadded)",$db);

我需要将结果放在这样的数组中(可以是长或短的月份名称,这不是问题):

$data = array(
    'Jan' => 12,
    'Feb' => 25,
    'Mar' => 0,
    'Apr' => 7,
    'May' => 80,
    'Jun' => 67,
    'Jul' => 45,
    'Aug' => 66,
    'Sep' => 23,
    'Oct' => 23,
    'Nov' => 78,
    'Dec' => 6
);

我正在尝试这个,但收到的消息不是数组:

$data = array();
while($graphData = mysql_fetch_array($graph)){
    $data[] = $graphData;
    }

我敢肯定这是一个简单的修复,但在这里扯掉头发!

4

3 回答 3

2

要获得预期的数组,您需要按照以下方式更改代码。

$data = array();
while($graphData = mysql_fetch_array($graph)){
    $data[$graphData['MONTH']] = $graphData['COUNT'];
}
于 2012-09-30T08:26:27.330 回答
0
$graph = mysql_query("SELECT MONTHNAME(dateadded) MONTH, COUNT(*) COUNT
                      FROM products
                      WHERE ((YEAR(dateadded)=2012) && (site_url = '$_GET[site_url_graph]'))
                      GROUP BY MONTH(dateadded)",$db);

$data = array();
while(list($month, $count) = mysql_fetch_array($graph)) {
    $data[$month] = $count; 
}

所以,基本上这是你想要的快速而轻松的版本。注意列表的用法。很好读。

于 2012-09-30T12:06:15.823 回答
-1

也许这就是问题所在:
COUNT(*) COUNT”?尝试将其更改为COUNT(*)

于 2012-09-30T08:20:32.747 回答