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());
$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);

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

This Post Has 0 Comments

Leave A Reply