d
Amit DhamuSoftware Engineer
 

MySQL to CSV Export

1 minute read 00000 views
$filename = 'file.csv';
$headers = ['Name', 'Age', 'Gender'];

$handle = fopen($filename, 'w');
fputcsv($handle, $headers, ',', '"');

$sql = mysql_query('SELECT * FROM users');

while($results = mysql_fetch_array($sql)) {
    $row = [
        $results['user_name'],
        $results['user_age'],
        $results['user_gender']
    ];
    fputcsv($handle, $row, ',', '"');
}

fclose($handle);