我在填充使用数据库中的值生成密钥的数组时遇到问题。数据库返回2行,第一行应该是键,第二行是该键的值。
这是我的代码:
$days = array();
if($query=db::query("select dayname(date_earned) as dayEarned, sum(downloads) as dayDownloaded
from perf_earnings
where date_earned between DATE_SUB(curdate(), INTERVAL 7 DAY) AND curdate()
group by dayEarned"))
{
while($row=$q->fetch_assoc()){
$day = $row["dayEarned"];
$dowSum = $row["dayDownloaded"];
$days[$day] = $dowSum;
}
}
我得到一个空数组。
我想得到的是:
array(
[Monday]=>230,
[Tuesday]=>500,
[Wednesday]=>340,
etc.
)
嗯,和往常一样,有一个愚蠢的错误,你不仔细阅读。我没有输入$ query,我想做fetch_assoc。
我写了" $ q"相反" $ query"。
非常感谢。
答案 0 :(得分:0)
我觉得你的查询没有获得任何结果集
第一件事
在orignial查询组中,需要 group by date_earned 而不是group by dayEarned
编写上述代码的更好方法是
<?php
$qry = "SELECT dayname(date_earned) as dayEarned, sum(downloads) as dayDownloaded
FROM perf_earnings
WHERE date_earned between DATE_SUB(curdate(), INTERVAL 7 DAY)
AND curdate()
GROUP BY date_earned";
$qid = mysql_query($qry);
if($qid->num_rows()){
while($row = mysql_fetch_array($qid)){
$day = $row["dayEarned"];
$dowSum = $row["dayDownloaded"];
$days[$day] = $dowSum;
}
}
?>