How to export data from mysql to csv or excel

Written on July 23, 2008 by turk

How to export data from mysql to csv or excel

This is a very simple code to export database from mysql database to file type excel format .csv or .xls.

Hear the PHPScript code

Example

<?php

mysql_connect($host,$user,$pass); // This line to connect database.
<!–[if !supportLineBreakNewLine]–>
<!–[endif]–>

mysql_select_db($dbn); // This line to select database name

$mysql=’SELECT * FROM database’; // This line to select database table

$rs_data=mysql_query($mysql);

while ($data=mysql_fetch_assoc($rs_data)){

$file = fopen(”contacts.xls”,”w”); // This line use function fopen() to create file type “w” which means write

 

$data_row=$data['firstname'].’,’.$data['lastname'].’,’.$data['address'].’,’.$data['te

lephone'].’\n’;

fputs($file,$data_row); // This line use function fputs() to insert data to file.

}

fclose($file); // This line for close file.

?>



If you enjoyed this post Subscribe to our feed

Leave a Reply