Script for import data from DB to CSV (works for UTF-8)

<?php
/*
Script will create CSV $file in next format "filed"{TAB}"fie""ld"{TAB}{NL}
Output example:
"POI NAME"	"CITY NAME"	"COUNTRY NAME"	
"Waag Amsterdam"	"Amsterdam"	"Netherlands"	
"Vondelpark Amsterdam"	"Amsterdam"	"Netherlands"	
...
*/
require_once('/home/alex/svn/global/global_conf.php');
require_once($EASYTOBOOK_GLOBAL_PATH."global/connectdb.php");
 
	$sql="SELECT `poi_name`,city_name,country_name
		  FROM `geo_poi`
		  LEFT JOIN geo_city ON poi_city_id=city_id
		  LEFT JOIN geo_country ON city_country_id=country_id;";
	$file="poi_full_list.csv";
	$elements=array("POI NAME","CITY NAME","COUNTRY NAME");
	$handle = fopen($file, "w");
	$title="";
	for($i=0;$i<count($elements);$i++) { $title.="\"".$elements[$i]."\"\t";	}
	fwrite($handle,$title."\n");
	$result = mysql_query($sql) or die("Query failed : " . mysql_error());
	$line=array();
	while($row=mysql_fetch_row($result)) {
		echo nl2br(print_r($row,TRUE));
		$title="";
		for($i=0;$i<count($row);$i++) {
			$title.="\"".trim(str_replace('"','""',$row[$i]))."\"\t";
		}
		fwrite($handle, $title."\n");
	}
	fclose($handle);
	echo "done.";
?>