How to get database table header information into an CSV File.

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-29T15:39:34Z Indexed on 2010/03/29 15:43 UTC
Read the original article Hit count: 297

Filed under:
|
|

I am trying to connect to the database and get current state of a table and update that information into csv file, with below mentioned piece of code, am able to get data information into csv file but am not able to get header information from database table into csv file.

So my questions is How can I get Database Table Header information into an CSV File ?

$config['database'] = 'sakila';
$config['host'] = 'localhost';
$config['username'] = 'root';
$config['password'] = '';

$d = new PDO('mysql:dbname='.$config['database'].';host='.$config['host'], $config['username'], $config['password']);
$query = "SELECT * FROM actor";
$stmt = $d->prepare($query);

// Execute the statement
$stmt->execute();

var_dump($stmt->fetch(PDO::FETCH_ASSOC));

$data = fopen('file.csv', 'w');

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo "Hi";
    // Export every row to a file
    fputcsv($data, $row);
}

Header information meaning:

Vehicle     Build   Model
 car        2009    Toyota
 jeep       2007    Mahindra

So header information for this would be Vehicle Build Model

Any guidance would be highly appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql