关于PDO::fetchAll()的问题太多了,但我仍然找不到答案。我需要获取由MySQL返回的结果。例如,如果我的列为id、name、age,则如下所示:
array(
"id"=array(1,2,3),
"name"=array("xy","by","cy"),
"age" = array(32,34,35)
)我知道我可以创建一个函数并遍历列表并手动将它们放在数组中,但我想知道是否有一种直接使用fetchAll(“魔术”)的方法。
发布于 2015-07-04 00:13:20
你可以这样做。
<?php
function returnResultAsArray()
{
$test=NULL;
// ..... connection and query here
$results = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($results as $row)
{
$test['id'][]=$row['id'];
$test['name'][]=$row['name'];
$test['age'][]=$row['age'];
}
return $test;
}
?>如果这对你有用,请告诉我。
https://stackoverflow.com/questions/31215412
复制相似问题