需要从$ i raw of mysqli object接收字段数据。
$res = $mysqli->query("SELECT mood,count(mood) as number FROM em_mood GROUP by mood ORDER by mood ASC");
$obj = $res->fetch_object();
for ($i = 1; $i <= 10; $i++){
if ($i==$obj->mood[$i]) $mood_result .= "<div class='chart'>".$obj->mood[$i].$obj->number[$i]."</div>";
else $mood_result .= "<div class='chart'>empty</div>";
}
说明: $ obj-&gt;心情等于1到10。 如果$ obj->情绪缺失&#34; 2&#34;例如,我需要用空div交换它。 换句话说,获取字段[情绪]值的语法是什么让我们说第二个原始?像$ obj [2] [心情]?
看起来像这样:
for ($i = 1; $i <= 10; $i++){
$res = $mysqli->query("SELECT mood,count(mood) as number FROM em_mood WHERE date>=CURDATE()-INTERVAL 30 DAY and mood=".$i);
$obj = $res->fetch_object();
echo $obj->mood." - ".$obj->number."<br>";
}
有没有办法让这更简单?不要做10个mysqli查询?
答案 0 :(得分:0)
$raw = [];
$sql = "SELECT mood,count(mood) as number FROM em_mood GROUP by mood";
$res = $mysqli->query($sql);
while ($obj = $res->fetch_object())
{
$raw[$obj->mood] = $obj->number;
}
现在您可以获得“原始”数据
for ($i = 1; $i <= 10; $i++){
if (isset($raw[$i])) {
$mood_result .= "<div class='chart'>$i - $raw[$i]</div>";
} else {
$mood_result .= "<div class='chart'>empty</div>";
}
}