Assuming that you KNOW the structure of the table, this should do it... if
you needed something more open-ended to cope with ANY table, I'd have to
think about it a little more:

<? // untested

// get each row of the table into a a var,
// separated by commas, ended with \n
$csv_contents = '';
$sql = "SELECT id,name,pass FROM tablename";
$result = mysql_query($sql);
while($myrow = mysql_fetch_array($result))
    // you have a row
    $csv_contents .= "{$myrow['id']},{$myrow['name']},{$myrow['pass']}\n";

function write_to_file($filename, $stringtowrite, $writetype)
    // lifted from the manual
    $filesession = fopen($filename,$writetype);

write_to_file('nameofmyfile.csv',$csv_contents, 'w');


This is all off the top of my head, untested, with snippets lifted from the
manual, but it should give u the theory, for which to build your own.

If your target OS's are likely to include Macs, you'll have to do some tests
on the line ending... \n or \r or perhaps \n\r will work best, with some

Justin French

Justin French

on 16/08/02 1:41 AM, Djurovski Dejan ([EMAIL PROTECTED]) wrote:

> Hi
> Can someone show me how to export MySQL table to a csv file?
> Thanks!

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to