+91-943-185-6038 me@shashidharkumar.com

phpThere are a couple of ways to export data from MySQL to a CSV file but some of them never supports adding a header row to the CSV which contains the column names. This post looks at how to export the data from MySQL into a CSV file with PHP including a header row.

The example code below uses the raw mysql_* functions but it should be easy enough to substitute a database library’s functions instead. It also writes the data out line by line to the CSV file whereas you could buffer the whole file in memory and write it out at one go; however if the resultset is large it may be better to write it out line by line so as not to consume too much memory.

The $server, $login, $password, $db and $table variables should be obvious in their purpose.



See also  How to fix blogger from redirecting to country specific domains?