我使用脚本将mysql数据库导出到.csv文件,但它也包含导出文件下面页面的所有html。
$fields = array();
$csv = array();
/*** mysql hostname ***/
$hostname = DB_HOST;
$dbname = DB_NAME;
/*** mysql username ***/
$username = DB_USER;
/*** mysql password ***/
$password = DB_PASS;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$tablename = 'comments';
$sql = 'SHOW COLUMNS FROM `'.$tablename.'`';
$stmt = $dbh->query($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
array_push($fields, $row['Field']);
}
array_push($csv, $fields);
$sql = 'SELECT `name`,`comment`,`email` FROM `'.$tablename.'`';
$stmt = $dbh->query($sql);
$stmt->execute();
$csv = array();
while($row = $stmt->fetch(PDO::FETCH_NUM))
{
array_push($csv, $row);
}
$fp = fopen('file.csv', 'w');
foreach ($csv as $row) {
fputcsv($fp, $row);
}
fclose($fp);
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=comments.csv");
header("Pragma: no-cache");
header("Expires: 0");
readfile('file.csv');
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
如果有人知道如何纠正这一点,我们将不胜感激。 感谢
答案 0 :(得分:0)
似乎
exit;
脚本停止将任何HTML输入.csv
后