i’m using the following code:
$select = "SELECT * FROM MyTable";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=contagiousbookings.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
which works fine BUT i have a date of birth field (field name of dob) which when spat out into the csv shows as 02041950 or something similar. Whats the best way to modify the above code so that it adds a dot between the days month and year i.e: 02.04.1950 or even a / would be great.
Thanks for any help.
You can format DATE field with a DATE_FORMAT function, e.g. –
Also, you could use SELECT…INTO OUTFILE syntax to produce CSV file.