I have a SELECT statement that will query MySQL database and then fwrite() to a .CSV file for download. To save as a CSV file, I have to put in some comas before fwrite() into the file. The problem is that many of the data strings in the database also contain comas. So when I open the CSV file in Excel, the data is not line up because of the extra comans that are already in the database with the strings. Is there a way to solve this problem or prevent this problem when fwrite() to a .csv file?
Any suggestion is much appreciated.
ljCharlie
How to solve problem with saving query result to CSV file?
Moderator: General Moderators
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
Many thanks for the quick response. Here's what I have.
But this doesn't work, like I said, the data in the database field contains a coma.
One other option is to save as tab delimited. However, I don't know what the php code for inserting a tab. Help on this issue is also welcome.
ljCharlie
Code: Select all
$dataContent = "".$rowї'LstName'].",".$rowї'MdnName'].",".$rowї'FstName'].",".$rowї'MdlName'].",".$rowї'NckName'].",".$rowї'FstMajor'].",".$rowї'FstYear'].",".$rowї'FstDegree'].",".$rowї'SndMajor'].",".$rowї'SndYear'].",".$rowї'SndDegree'].",".$rowї'Email'].",".$rowї'Website'].",".$regDate."\n";One other option is to save as tab delimited. However, I don't know what the php code for inserting a tab. Help on this issue is also welcome.
ljCharlie
-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
like this
\" escapes the quote so it ends up like
"var","var2",
and excel will treat it like one big string, ignoring the commas
Code: Select all
$dataContent = """.$rowї'LstName']."","".$rowї'MdnName']."", etc.."var","var2",
and excel will treat it like one big string, ignoring the commas
hm.... you can try:ljCharlie wrote:Many thanks for the quick response. Here's what I have.
Code: Select all
$dataContent = "".$rowї'LstName'].",".$rowї'MdnName'].",".$rowї'FstName'].",".$rowї'MdlName'].",".$rowї'NckName'].",".$rowї'FstMajor'].",".$rowї'FstYear'].",".$rowї'FstDegree'].",".$rowї'SndMajor'].",".$rowї'SndYear'].",".$rowї'SndDegree'].",".$rowї'Email'].",".$rowї'Website'].",".$regDate."\n";
Code: Select all
$dataContent = '"' . implode('","', $row) . '"';Tabs are denoted as \t in double quoted stringsljCharlie wrote: One other option is to save as tab delimited. However, I don't know what the php code for inserting a tab. Help on this issue is also welcome.