Solución:
SELECT * INTO OUTFILE "c:/mydata.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "n"
FROM my_table;
(la documentación para esto está aquí: http://dev.mysql.com/doc/refman/5.0/en/select.html)
o:
$select = "SELECT * FROM table_name";
$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=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$headern$data";
Mira esta pregunta / respuesta. Es más conciso que el de @ Geoff y también usa la función fputcsv incorporada.
$result = $db_con->query('SELECT * FROM `some_table`');
if (!$result) die('Couldn't fetch records');
$num_fields = mysql_num_fields($result);
$headers = array();
for ($i = 0; $i < $num_fields; $i++) {
$headers[] = mysql_field_name($result , $i);
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
Mire la documentación relacionada con la sintaxis SELECT … INTO OUTFILE.
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY 'n'
FROM test_table;
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)