如何使用PHP导出Excel / CSV中的Mysql数据表? [英] How to export Mysql data table in Excel/CSV using PHP ?

查看:80
本文介绍了如何使用PHP导出Excel / CSV中的Mysql数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了以下代码来生成excel中的报告,但它只显示网页上的标题,甚至没有生成excel文件。请在这方面帮助我。



I have written the below code to generate a report in excel but it is only showing the headers on webpage,not even generating excel file.please help me in this regards.

<?php
error_reporting(0);
  include("database.php");


    $fromdate= $_GET['fromdate'];
    $todate = $_GET['todate'];
    echo $fromdate;
//First we'll generate an output variable called out. It'll have all of our text for the CSV file.
$out = '';

 $csv_hdr .= "\n\n\n Id,Date,Time,Movie,Name,Email,Phone,Approved";

 $out .= $csv_hdr;
 $out .= "\n\n";
 $result1 = mysql_query("SELECT * from bookings where (date)date between '$fromdate' and '$todate'");

 while($row = mysql_fetch_array($result1))
            {
                 $id= $row["id"];
                 $date= $row["date"];
                 $time= $row["time"];
                 $movie= $row["movie"];
                 $name= $row["name"];
                 $email= $row["email"];
                 $phone= $row["phone"];
                 $approved= $row["approved"];
            }
             $csv_output .= $id .", ".$date.", ".$time.", ".$movie.", ".$name.", ".$email.", ".$phone.", ".$approved;
             $csv_output .= "\n";
             $out .= $csv_output ."\n\n";

$filename = "FirstLook-$fromdate-$todate";

//Generate the CSV file header

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: attachment; filename=".$filename.".csv");

//Print the contents of out to the generated file.
echo "\n                                                               "." Report of FirstLook Preview";

print $out;
exit;
?>





新更新:何时被评论(0)使用error_reporting;我收到这些错误:







NEW UPDATE:when is commented error_reporting(0); I get these errors:


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/vrwebqbj/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 39
Report of FirstLook Preview Id,Date,Time,Movie,Name,Email,Phone,Approved , , , , , , ,

推荐答案

fromdate =
fromdate=


_GET [' fromdate'];
_GET['fromdate'];


todate =
todate =


这篇关于如何使用PHP导出Excel / CSV中的Mysql数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆