export mysql db into text file

hi everyone
I want to know
how to export table data from a mysql database into text file using php?

thanks in advance

There are several ways you can do this. The shortest is:
[php]mysql_query(“SELECT Field1, Field2 INTO OUTFILE ‘/home/public_html/tmp/data.csv’ FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’ FROM MyTable”);[/php]

But this will work if writing permissions are set for MySQL to the corresponding directory on server.

Second way, is just creating a file from your php script and writing there all the table records in a loop.
Here is an example:
// read data from a database table
$r = mysql_query(“SELECT Field1, Field2 FROM MyTable ORDER BY Field1”);

// create file for output
    $line=$f["Field1"].", ".$f["Field2"];
    if(trim($line)!='') fputs($fp,$line."\n");


thank you so much

Sponsor our Newsletter | Privacy Policy | Terms of Service