PHP - Fastest way to download Table data to a CSV - Large Data
<p>I have a large table with 500,000 records, on clicking of a button the data gets downloaded, but it is very slow especially on a bad internet.</p>

<p>I was thinking of Zipping the file and then save it but again I am sure it will take up extra memory for the whole process.</p>

<p>Is there a better way to optimize this CSV download.</p>

<pre><code>&lt;?php
        // mysql database connection details
        $host = "localhost";
        $username = "admin";
        $password = "root";
        $dbname = "db_books";

        // open connection to mysql database
        $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));

        // fetch mysql table rows
        $sql = "select * from tbl_books";
        $result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));

        $fp = fopen('books.csv', 'w');

        while($row = mysqli_fetch_assoc($result))
        {
            fputcsv($fp, $row);
        }

        fclose($fp);

        //close the db connection
        mysqli_close($connection);
    ?&gt;
</code></pre>