I am trying to fetch records from MySql and display them in an array along with column headers using PDO, reason I am doing this is because i want to export them in an excel and I am using php-excel
right now the following code displays the records perfectly fine in an array and I am able to export them just fine
while ($row[] = $stmt->fetch(PDO::FETCH_ASSOC)) :
$data = $row;
endwhile;
The array i get as a result of above code is as following
Array
(
[0] => Array
(
[user_id] => 1
[fname] => First Name
[lname] => Last Name
)
[1] => Array
(
[user_id] => 91
[fname] => First Name
[lname] => Last Name
)
)
But the problem is I am missing column headers in excel, so for that I need to display the MySql column headers in an array as well.
I am using the following code to create an array for column headers
for ($i = 0; $i < $stmt->columnCount(); $i++) {
$col = $stmt->getColumnMeta($i);
$columns[] = $col['name'];
}
but this gives me an array in following format
Array
(
[0] => user_id
[1] => fname
[2] => lname
)
whereas I need the column header array in following format
Array
(
[0] => Array
(
[user_id] => user_id
[fname] => fname
[lname] => lname
)
)
I will appreciate any help.