Re: MySQL to csv

From: Date: Thu, 15 Aug 2002 16:02:08 +0000
Subject: Re: MySQL to csv
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-112788@lists.php.net to get a copy of this message
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); fwrite($filesession,"$stringtowrite"); fclose($filesession); } 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 testing. Justin French Justin French on 16/08/02 1:41 AM, Djurovski Dejan (klavir@ptt.yu) wrote: > Hi > > Can someone show me how to export MySQL table to a csv file? > > Thanks! > >

« previous php.general (#112788) next »