我已经适应了这个网站的信息,但不是创建一个csv文件,它只是在命令屏幕上显示它正在执行的信息。我试过进去 $fileName = 'C:\Users\dmcgettigan\Desktop\mysql-export.csv';
只有文件名,但我没有生成文件。提前谢谢你的帮助,我正在努力自学php和mysql!
更新:添加代码
我的代码:
<?php
//Our MySQL connection details.
$host = 'mysql_server';
$user = 'user';
$password = 'password';
$database = 'database';
//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);
//Create our SQL query.
$sql = "SELECT
a.InvoiceNumber, a.partnumber, a.Quantity, b.Discount, date
FROM
data a,
mars b
WHERE
a.PartNumber = b.partnumber
AND date >= '2018-09-28'
AND mfg = 'gk'
AND discount <> '0.00'
AND CustomerNumber IN ('Z5447520' , 'Z3715177', 'Z1234444', 'Z5425966')
AND Quantity > '0'";
//Prepare our SQL query.
$statement = $pdo->prepare($sql);
//Executre our SQL query.
$statement->execute();
//Fetch all of the rows from our MySQL table.
$rows = $statement->fetchAll(PDO::FETCH_ASSOC);
//Get the column names.
$columnNames = array();
if(!empty($rows)){
//We only need to loop through the first row of our result
//in order to collate the column names.
$firstRow = $rows[0];
foreach($firstRow as $colName => $val){
$columnNames[] = $colName;
}
}
//Setup the filename that our CSV will have when it is downloaded.
$fileName = 'mysql-export.csv';
//Set the Content-Type and Content-Disposition headers to force the download.
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
//Open up a file pointer
$fp = fopen('php://output', 'w');
//Start off by writing the column names to the file.
fputcsv($fp, $columnNames);
//Then, loop through the rows and write them to the CSV file.
foreach ($rows as $row) {
fputcsv($fp, $row);
}
//Close the file pointer.
fclose($fp);
1条答案
按热度按时间v64noz0r1#
$fp = fopen('php://output', 'w');
此特定行应更改为$fp = fopen($filename, 'w');
因为按原样使用输出作为文件