export a mysql table to csv with column headers

You should build Your columns schemat in array. Than add this on the top of csv and than You can add content of Your csv.

$string_headers="post_title,post_author,etc....";

if(count($results) > 0)
{
    $result = $string_header;
    foreach($results as $result){
    $tmp_result = array_values($result);
    $result .= implode(", ", $tmp_result);
    $csv_output .= $result."\n";
}