tags:

views:

176

answers:

4

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.

+2  A: 

Using SHOW COLUMNS FROM will get you the column names from . You then can write the first line of your CSV with them:

// Do this before adding the rows from the database
$query = "SHOW COLUMNS FROM some_table";
$stmt = $d->prepare($query);
$stmt->execute();
$columns = '';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    $columns .= $row['Field'] . ',';
}
$columns = substr($columns, 0, -1);
fputcsv($data, $columns);
John Conde
Can you explain as how can I write first line of CSV file with them, coz right now it is not being updated into CSV file ?
Rachel
Edited my answer to better demonstrate how this is done.
John Conde
Yes. I am going through it right now, thanks for detailed explanation.
Rachel
No need for an extra query, just get the information from the array $row
Frank Heikens
+1  A: 

Use mysqldump:

mysqldump -u user -p --fields-terminated-by=',' --tab=/tmp mydatabase mytable

The above command will create 2 files in /tmp. mytable.sql and mytable.txt. The sql file will contain the table creation schema and the txt file will contain the records of the mytable table with fields delimited by a comma.

OMG Ponies
it only created .sql file and not .txt file, am I missing out something here.
Rachel
+5  A: 

Just get the keys from the array $row: array_keys(). Now you have all the columnnames and you can put them in your csv-file.

======= EDIT =======

<?php

$header = array();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    if(empty($header)){ // do it only once!
      $header = array_keys($row); // get the columnnames
      fputcsv($data, $header); // put them in csv
    }

    echo "Hi";
    // Export every row to a file
    fputcsv($data, $row);
}
?>
Frank Heikens
Would appreciate if you can you explain this in little detail as I am not getting it right now.
Rachel
See the edited answer
Frank Heikens
@Frank: How would it know if it is an header or not ?
Rachel
Yes. This approach worked for me. Thanks
Rachel
@Frank: This approach worked but I am not sure what is happening with header, can you explain as to how this would work in details, possibly via comments so that I can get better understanding of it so that I can learn from it.
Rachel
Rachel, the function array_keys() gets all the keys from the array $row. In my example, array_keys() fills the array $header and this array is writen in your csv. Because of the if{}, this is done only once. See also the hyperlink to the PHP-manual.
Frank Heikens
@Frank: Thank you Frank, now I understood it. Appreciate your guidance with it.
Rachel
A: 

You already have it in PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

EDIT: One way to get and write the names of columns is (put it before your while lopp)

fputcsv($data, array_keys($stmt->fetch(PDO::FETCH_ASSOC)));

Read about php arrays and associative arrays in particular and on function on arrays (such as array_keys). Answers to particular questions can not replace understanding of basic concepts such as arrays.

Unreason
Than how do I get that header information out, am not sure as to how we can work with database header information.
Rachel