[英]Download a CSV file from MYSQL with PHP POST
我無法創建腳本來下載CSV文件。 每次我運行腳本時,它都會中斷。 我沒有收到任何錯誤。 該頁面進入腳本后即停止加載。 我環顧四周,發現了一些答案,但似乎都沒有用。 我是php的新手,如果您知道這里發生了什么,將不勝感激。 提前致謝!
if (isset($_POST['downloadCSV'])) {
$list = null;
include 'classes/Credentials.php';
try
{
// Include globals from credentials.
global $dbname, $host, $user, $pass;
// Set up on database switch
$conn = new PDO("mysql:dbname=$dbname;host=$host", $user, $pass);
$conn->exec("SET CHARACTER SET utf8");
} catch(PDOException $e) {
echo $e->getMessage();
}
// Define and perform the SQL SELECT query
$sql = 'SELECT * FROM rental';
$result = $conn->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false)
{
$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));
}
$conn = null; // Disconnect
die;
}
}
}
}
嘗試
// If the SQL query is succesfully performed ($result not false)
if($result != false) {
您正在使用的比較運算符正在檢查“不相同”和“不相等”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.