Blog

Export Mysql database to CSV File Using PHP

There is Built in Function in php to achieve this fputcsv() – Format line as CSV and write to file pointer

mysql_connect(‘localhost’, ‘root’, ‘password’) or die(“Could not connect: ” . mysql_error());
mysql_select_db(“shoppers”);
$result = mysql_query(“SELECT * FROM `orders`”);
$fetch = array();
while($row = mysql_fetch_array($result)){
$fetch[] = $row;
}

$list = $fetch;
$fp = fopen(‘file.csv’, ‘w’);
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

Also For Import Go for fgetcsv() – Gets line from file pointer and parse for CSV fields

http://www.php.net/manual/en/function.fgetcsv.php

This Post Has 0 Comments

Leave A Reply