2

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.

3 Answers 3

1

I assume you want the headers to be in the same array. In that case you would do this before the while loop

$data[0] = array();
for ($i = 0; $i < $stmt->columnCount(); $i++) {
    $col = $stmt->getColumnMeta($i);
    $data[0][$col['name']] = $col['name'];
}

Also, the while loop is a little inefficient. You're pushing each row into the $row array and then copying it into $data every single time. It should be done like this instead:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
    $data[] = $row;
endwhile;

This should get you:

$data = Array
(
    [0] => Array
    (
        [user_id] => user_id
        [fname] => fname
        [lname] => lname
    )
    [1] => Array
    (
        [user_id] => value...
        ...
    )
    ...
)
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of letting it auto-number, just provide the name again as the key:

for ($i = 0; $i < $stmt->columnCount(); $i++) {
    $col = $stmt->getColumnMeta($i);
    $columns[$col['name']] = $col['name'];
}

Example: https://eval.in/117424

Comments

1

Might be easier:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if(!isset($data)) {
        $data[] = array_combine(array_keys($row), array_keys($row));
    }
    $data[] = $row;
}

Or you could add them after the fact using array_unshift().

Or in a separate array:

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    if(!isset($col)) {
        $col[] = array_combine(array_keys($row), array_keys($row));
    }
    $data[] = $row;
}

1 Comment

Indeed another way of doing it, that's why I love stackoverflow, brilliant solutions by brilliant minds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.