$value) { $header_string .= $key."$header_seperator"; //why is there no str_cat function... $string .= "\"$value\"$seperator"; } //chop off tail of header $strlen = strlen($header_string) -1; //hack to get around order of precedence issues $header_string = substr($header_string,0,$strlen); //likewise, CSV field $strlen = strlen($string) - 1; $string = substr($string,0,$strlen); //Write to file fwrite($file,$header_string."\n"); fwrite($file,$string."\n"); } //Build CSV while ($row = mysql_fetch_row($query)) { $string = ""; //Fetch all rows till the last one for ($i = 0; $i < (count($row)); $i++) { if ($row[$i] == NULL && $listnull) $row[$i] = "NULL"; $string .= "\"$row[$i]\"$seperator"; } //Last row, without the seperator $string .= "\"$row[$i]\""; //Write string to file fwrite($file,$string."\n"); } #################### ## Functions ## #################### function error($str) { echo "Error: $str"; die(); } ?>