如何调试PHP错误“第48行的文件意外结束”?

时间:2013-12-11 12:01:27

标签: php mysql database excel export

虽然我只有47行代码,但我仍然收到错误“第48行文件的意外结束”。我不知道我的代码中是否存在问题,或者这是否是将数据库查询导出为CSV文件的正确代码。

<html>
    <head></head>
    <body>

        <form method="POST" action="">
        DATE FROM: <input type="date" name="datefrom"> TO: <input type="date" name="dateto"> <input type="submit" value="Extract excel file" name="extract"></input>
        </form>

        <?php   
        header("Content-type: text/csv; charset=UTF-8");
        header('Content-Disposition: attachment; filename=Export.csv');

        if(isset($_POST['extract'])){
            $con = mysql_connect("localhost", "root");
            if(!$con){
                echo "Error connection";
                    }

            $select_db = mysql_select_db('sample', $con);
            if(!$select_db){
                echo "Error to select database";
                }
            mysql_set_charset("utf8", $con);

          $datefrom = $_POST['datefrom'];
          $dateto = $_POST['dateto'];

        $myquery = mysql_query("SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY empno");

        //While loop to fetch the records
        $contents = "id_biometrics,empno,date_created,time_created,status\n";
        while($row = mysql_fetch_array($myquery))
        {
            $contents.=$row['0'].",";
            $contents.=$row['1'].",";
            $contents.=$row['2'].",";
            $contents.=$row['3'].",";
            $contents.=$row['4']."\n";

        }

        $contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
        print $contents_final;

        ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

您的if(isset($_POST['extract'])){ if永远不会关闭,因此您需要在适用的位置添加}(我认为在最终打印后)