-1

I keep getting the error "unexpected end of file in line 48" although I only have 47 lines of code. I don't know if there's a problem in my code or if this is the right code to export my database query to CSV file.

<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>
halfer
  • 19,824
  • 17
  • 99
  • 186
nicole101
  • 187
  • 3
  • 20
  • **Warning:** you're using [a **deprecated** database API](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) yourself from. And well, what do you get? Did you actually try your code? – Marcel Korpel Dec 11 '13 at 12:01
  • 2
    Use PHP's built-in [fputcsv()](http://www.php.net/function.fputcsv) function for creating comma-separated value file – Mark Baker Dec 11 '13 at 12:04
  • I haven't tried it because I keep getting an error. :( – nicole101 Dec 11 '13 at 12:12

1 Answers1

2

Your if(isset($_POST['extract'])){ if is never closed, so you need to add an } where applicable (i think after the final print)

Ronald Swets
  • 1,669
  • 10
  • 16