1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php include('cmdataconn.inc'); $result = mysql_query("SELECT * FROM mytable WHERE client='Dog'"); header('Content-Type: text/csv'); header('Content-Disposition: attachment;filename=export.csv'); $row = mysql_fetch_assoc($result); if ($row) { echocsv(array_keys($row)); } while ($row) { echocsv($row); $row = mysql_fetch_assoc($result); } function echocsv($fields) { $separator = ''; foreach ($fields as $field) { if (preg_match('/\\r|\\n|,|"/', $field)) { $field = '"' . str_replace('"', '""', $field) . '"'; } echo $separator . $field; $separator = ','; } echo "\r\n"; } ?> |
