2015-11-23 33 views
0

我想導出我的聯繫電子郵件,存儲在MySQL數據庫與腳本。 我需要導出我的電子郵件在CSV文件。從php導出電子郵件與php,但出口我所有的PHP頁面

但是,當頁面重新加載,文件正在下載並進入這個文件我有我所有的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" />.....) 

謝謝

+0

'die(); // while while循環之後 – Steve

+0

你有一個{之後缺少 – Mihai

+1

@Mihai花括號不需要單線循環結構 – Steve

回答

0

您正在打開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'); 
} 
?> 
0

我解決了這個問題:)

我把我的腳本在外部PHP文件確切的結果,和它的作品! 之前我的腳本是在我的頁面頂部,並沒有工作

謝謝!

相關問題