我尝试将SQL查询结果插入PHP数组:
$sql= $conn -> prepare("SELECT top(10) code, name, SUM(minutes)as total FROM db_name group by code, name ORDER BY total desc");
$sql -> execute();
$combinedResults = array();
while ($row = $sql -> fetch(PDO::FETCH_ASSOC)) {
$combinedResults[$row['code']][] = array(
'Name' => $row['name'],
'Total' => $row['total'] );
};
echo($combinedResults['code'][0]['Total']);
?>
并在echo行中返回脚本Undefined index: code
答案 0 :(得分:1)
错字:
更改
$combinedResults[$row['code']][] = array(
到
$combinedResults['code'][$row['code']] = array(
答案 1 :(得分:0)
这里是您的"代码" index必须是数据库中的值。这就是为什么关联数组中没有这样的索引的原因。或者,如果您需要"代码"索引,你需要像这样重写(如果我已经正确理解):
while ($row = $sql -> fetch(PDO::FETCH_ASSOC)) {
$combinedResults['code'][] = array(
'Name' => $row['name'],
'Total' => $row['total'] );
};