I have a script that takes a SQL Statement and puts the query result into a CSV file. Right now it only the rows of the table, I want it to Put the headings of the table at the top. How would I do that with this current script?
Script...
<?php
include 'connect.php';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment;filename="export.csv"');
header('Cache-Control: max-age=0');
$fpcsv = fopen('php://output', "a+");
$sqlstatement = $_GET['sqlstatement'];
$exportcsv_q = mysql_query($sqlstatement);
if (@mysql_num_rows($exportcsv_q) > 0) {
$campos = mysql_num_fields($exportcsv_q);
while ($exportcsv_r = mysql_fetch_row($exportcsv_q)) {
fputcsv($fpcsv, $exportcsv_r);
}
}
exit;
?>
If anyone out there has a mysqli version of this I would love to be able to switch this over.