Hi all,
Can anyone tell me how to fix the following problem.
Below in a section of a sql to csv script i am working on:
if (($go) && ($sql2csv)) {
mysql_pconnect($db_host, $user_nm, $password) or die( "Unable to connect to SQL server");
@mysql_select_db($database) or die( "Unable to select database");
if($table == 'approved'){
$sqlcont = "SELECT * FROM company_details WHERE type_email = 'accepted' AND client_update='yes'";
} else if($table == 'payment_details'){
$sqlcont = "select * from payment_details";
}
$result = mysql_query($sqlcont);
echo mysql_error();
function make_csv_happy($string,$seperator) {
$string = trim($string);
if (eregi("\$seperator",$string)) {
$string = ereg_replace("\"", "\"\"", $string);
$string = "\"".$string."\"";
}
$string = ereg_replace(10, "", $string);
$string = ereg_replace("\r", "", $string);
return $string;
}
if (mysql_fieldname($result, 0) == "id") $first_field = "iD";
while($col < mysql_numfields($result)) {
$fname = mysql_fieldname($result, $col);
if ($col < mysql_numfields($result)-1)$comma = $seperator;
else $comma = "";
if (($col == 0) && ($first_field))
$names .= $first_field.$comma;
else
$names .= make_csv_happy(strtoupper($fname),$seperator).$comma;
$col++;
}
$final = $names."\n";
while($row < mysql_numrows($result)) {
$col=0;
$line = "";
while($col < mysql_numfields($result)) {
$fname = mysql_fieldname($result, $col);
if ($col < mysql_numfields($result)-1)$comma = $seperator;
else $comma = "";
$line .= make_csv_happy(mysql_result($result,$row,$fname),$seperator).$comma;
$col++;
}
$final .= $line."\n";
$row++;
}
if ($send) {
header("Content-disposition: filename=$table.csv");
header("Content-type: application/octetstream");
header("Pragma: no-cache");
header("Expires: 0");
$client=getenv("HTTP_USER_AGENT");
if (ereg('[^(]*\((.*)\)[^)]*',$client,$regs)) {
$os = $regs[1];
if (eregi("Win",$os)) $crlf="\r\n";
}
} else {
echo "</center><p align=\"left\"><pre>";
}
echo $final;
if (!$send) echo "</pre></p>";
}
Everything works fine except if a field in the database contains a "," it produces another column in the csv file.
Because i am using the "," as my seperator how can i get my csv results to display correctly?
I have tried ereg_replace(),preg_replace() to no avail.
Can any help me out on this one.
Cheers,
micmac