简体   繁体   中英

Proeblem in exporting mysql data to .csv via php

I use the following way to try export the data to a csv file, the methoid I am using is from the below link Problem in Export csv file in php , but when I try to export the file, the system replys me no such file can be read, so I would like to ask I have to pre-open a result.csv first before I execute the code? Or the file result.csv has already generated, but in a wrong directory, if this is the reason, may I ask how can I define the directory the file should be created in.

$result = mysql_query("SELECT TechName, ClientName, SiteName, Time, Type
                            INTO OUTFILE 'result.csv'
                            FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED by '\"'
                            LINES TERMINATED BY '\n'
                            FROM Tech AS T, Client AS C, Site AS S, Log AS L
                            WHERE T.TechID=L.TechID AND C.ClientID=L.ClientID AND S.SiteID=L.SiteID
                            ORDER BY L.Time DESC");
    $Time = date('Y_m_d_H_i');
    $fileName = "Report_".$Time.".csv";
    header('Content-type: text/csv'); 
    header('Content-Disposition: attachment; filename="'.$fileName.'"'); 
    readfile('result.csv');

Try this code may be. Found it here: http://salman-w.blogspot.in/2009/07/export-mysql-data-to-csv-using-php.html

<?php
/*
 * PHP code to export MySQL data to CSV
 * http://salman-w.blogspot.com/2009/07/export-mysql-data-to-csv-using-php.html
 *
 * Sends the result of a MySQL query as a CSV file for download
 */

/*
 * establish database connection
 */

$conn = mysql_connect('MYSQL_HOST', 'MYSQL_USERNAME', 'MYSQL_PASSWORD') or die(mysql_error());
mysql_select_db('MYSQL_DATABASE', $conn) or die(mysql_error($conn));

/*
 * execute sql query
 */

$query = sprintf('SELECT * FROM MYSQL_TABLE');
$result = mysql_query($query, $conn) or die(mysql_error($conn));

/*
 * send response headers to the browser
 * following headers instruct the browser to treat the data as a csv file called export.csv
 */

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=export.csv');

/*
 * output header row (if atleast one row exists)
 */

$row = mysql_fetch_assoc($result);
if ($row) {
    echocsv(array_keys($row));
}

/*
 * output data rows (if atleast one row exists)
 */

while ($row) {
    echocsv($row);
    $row = mysql_fetch_assoc($result);
}

/*
 * echo the input array as csv data maintaining consistency with most CSV implementations
 * - uses double-quotes as enclosure when necessary
 * - uses double double-quotes to escape double-quotes 
 * - uses CRLF as a line separator
 */

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";
}
?>

If you are trying to output the result of your query, simply do so. No need for readfile() at all. All you need are the headers that you already have, and something to output your CSV data.

Also note that you can simply use fputcsv() to format your array as a CSV line for you. http://php.net/manual/en/function.fputcsv.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM