Php PDO fetchAll (), simple result
So many questions about PDO :: fetchAll (), but I can't seem to find an answer. I need to get my results as it is returned by MySQL. for example, if I have columns id
, name
, age
to return as follows:
array(
"id"=array(1,2,3),
"name"=array("xy","by","cy"),
"age" = array(32,34,35)
)
I know I can execute a function and iterate over the list and put them into the array manually, but I want to know if there is a direct way to use fetchAll ('magic').
+3
source to share
1 answer
You can do it.
<?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;
}
?>
Let me know if it works for you
+3
source to share