使用php从mysql导出电子邮件,但导出所有我的php页面

使用php从mysql导出电子邮件,但导出所有我的php页面,php,mysql,csv,download,export,Php,Mysql,Csv,Download,Export,我想导出我的联系人电子邮件,用脚本存储在mysql数据库中。 我需要在csv文件中导出我的电子邮件 但是,当页面重新加载时,文件正在下载,我将所有的php页面都下载到这个文件中 <?php if(IsSet($_POST['export_test'])){ // output headers so that the file is downloaded rather than displayed header('Content-Type: text/csv; charset

我想导出我的联系人电子邮件,用脚本存储在mysql数据库中。 我需要在csv文件中导出我的电子邮件

但是,当页面重新加载时,文件正在下载,我将所有的php页面都下载到这个文件中

<?php
if(IsSet($_POST['export_test'])){
    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // output the column headings
    fputcsv($output, array('E-mail'));

    // fetch the data
    $string = "SELECT Email FROM address";
    $query = mysql_query($string);

    // loop over the rows, outputting them
    while ($row = mysql_fetch_assoc($query)) fputcsv($output, $row);
}
?>

在data.csv中,我可以看到我的所有页面

(<!DOCTYPE html>
<html lang="en" ng-app>

<head>
    <title>Test</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700,300' rel='stylesheet' type='text/css'>
<link rel="shortcut icon" href="icon/advancedsettings.png" type="image/x-icon" />.....)
(
试验
.....)

谢谢

您正在打开csv文件并编写一些内容,然后执行查询。那你怎么才能把这些邮件作为输出呢?。试试这段代码,它会给出你需要的准确结果

<?php
$conn=mysqli_connect("localhost","root","","table_name");// connection to db
if(isset($_POST['export_test'])){
$sql="select email from address";// select query 
$res=mysqli_query($conn,$sql);
$line .= "\n"; // new line 
$filename='email.csv';// create csv file if dosent exist 
$fp = fopen($filename, "w");// open the csv file to write 
while($row=mysqli_fetch_array($res)){
$line = "";
$comma = "";
$line .= $comma . '"' . str_replace('"', '""', $row['email']) . '",';
$comma = ",";
$line .= "\n";
fputs($fp, $line); // put the line into csv 
}
fclose($fp);
header('Content-Type: text/csv; charset=utf-8');// to download the email.csv
header('Content-Disposition: attachment; filename=email.csv');
}
?>

我解决了这个问题:)

我把我的脚本放在一个外部php文件中,它就可以工作了! 在我的脚本出现在我的页面顶部并且不起作用之前


谢谢大家!

die()//在while循环之后
您有一个{在while循环之后丢失while@Mihai单线循环不需要花括号。你能显示连接吗?你试过我的答案了吗