Hi,
Im using a script to export a csv file from mySQL.
Everything works fine, however the data comes out as tab delimited and I need it to be comma delimited. Can anyone help? Please?
Here is the script:
<?php
include '../library/config.php';
include '../library/opendb.php';
$time = time();
// Query DB
$select = "SELECT name, email FROM tablename";
$export = mysql_query($select);
$row_export = mysql_fetch_assoc($export);
$fields = mysql_num_fields($export);
// Is there any data or should we call it quits?
$num_rows = mysql_num_rows($export);
if (!$num_rows){
echo "<h1 style=\"font-family: Arial, Helvetica, sans-serif\">Sorry, no data available for export.</h1>";
exit();
}
// Set headers
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=7yarra_mail_". date("dmy") .".csv; size=$size_in_bytes");
header("Pragma: no-cache");
header("Expires: 0");
// Process rows
$count = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
if ($num_rows){
mysql_data_seek($export,0);
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
}
else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
echo $header."\n".$data;
include '../library/closedb.php';
?>
Thanks,
Melz