我试图找出为什么我的查询不起作用。我正在尝试将status = 'S'
的每个月的所有金额加在一起。但是,我收到以下错误。有什么想法吗?
[05-Jul-2013 11:21:30 America/New_York] PHP Fatal error: Cannot use object of type mysqli_result as array
我的代码:
$closedsales = mysqli_query($mysqli, "SELECT MONTH(date) as month, sum(amount) as total FROM sales WHERE user_id = '".$userid."' AND status = 'S' GROUP BY MONTH(date)");
while ( $row = mysqli_fetch_row($closedsales) ) {
$closedsales[$row['month']] = $row['total'];
}
更新:
$closedsales = mysqli_query($mysqli, "SELECT MONTH(date) as month, sum(amount) as total FROM sales WHERE user_id = '".$userid."' AND status = 'S' GROUP BY MONTH(date)");
while ( $row = mysqli_fetch_row($closedsales) ) {
$monthlysales[$row['month']] = $row['total'];
}
foreach($monthlysales as $monthlysale) {
echo $monthlysale;
echo "This worked...";
}
谢谢!我摆脱了那个错误。但是,由于某种原因,我的数组不包含任何内容。它只打印出This worked...
这是否可能是因为我在DB中没有每个月?
答案 0 :(得分:2)
问题在于作业:
$closedsales[$row['month']] = $row['total'];
$closedsales
是mysqli_query
返回的结果,它不是您可以分配的数组。为此使用不同的变量。