| 
	
 | 
 Posted by Marcin Dobrucki on 10/24/06 11:55 
Steven Paul wrote: 
> I need to write the output of an SQL query as comma-separated values. 
> So I wrote this, which is almost right: 
>  
>       while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) 
>       { 
>            $rows[] = $row; 
>       } 
>       foreach( $rows[0] as $key => $value) 
>       { 
>          echo $key.","; 
>       } 
>       echo "\n"; 
>        
>       foreach ($rows as $row) 
>       { 
>          foreach( $row as $key => $value) 
>          { 
>             echo $value.","; 
>          } 
>          echo "\n"; 
>       } 
 
   You are reinventing the wheel: 
 
SELECT * INTO OUTFILE "/tmp/outfile.csv" FIELDS TERMINATED BY "," FROM  
<yourtable>; 
 
   There are also a number of options on enclosing fields, specifying  
newlines and so on. 
 
   /m
 
  
Navigation:
[Reply to this message] 
 |