É 2017 e o mysqli é mais comum que o mysql agora. Então aqui está uma versão mysqli da resposta de Josh Liptzin:
<?php
/* Attempt MySQL server connection. */
$connection = mysqli_connect($database_server, $database_username, $database_password, $database_name);
// Check connection
if($connection === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$query = "SELECT * FROM Employee_data";
$result = mysqli_query($connection, $query);
$number_of_fields = mysqli_num_fields($result);
$headers = array();
for ($i = 0; $i < $number_of_fields; $i++) {
$headers[] = mysqli_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
function mysqli_field_name($result, $field_offset)
{
$properties = mysqli_fetch_field_direct($result, $field_offset);
return is_object($properties) ? $properties->name : null;
}
?>