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