Hi,
I was wondering if someone can help me here. I have written some PHP code to download the contents from a mysql table to a CSV file. My problem is that some of the fields in the CSV have leading blank spaces and some do not. It happens with both varchar and text fields – but if numeric values are contained in the mysql database in any of these fields then it does not happen. No leading blanks are stored in the mysql database and I would like to get rid of the leading blanks in the CSV download!!
Anyway here is a sample of my code – can anyone suggest something that I am doing here to cause my problem so I can fix it:
[code=php]
header(‘Content-type: text/csv’);
header(‘Content-Disposition: attachment; filename=”filename.csv”‘);
echo “HEADING1, HEADING2, HEADING3n”;
$query = “SELECT field1, field2, field3 FROM table1 WHERE field1 = ‘$field1′”;
$result = mysql_query($query) or die (“MySQL error: $query. ” . mysql_error());
while ($myrow = mysql_fetch_array($result)) {
$field1=$myrow[field1];
$field2=$myrow[field2];
$field3=$myrow[field3];
echo “$field1, $field2, $field3n”;
}
In this example field1 is a numeric field (defined as type text in the mysql database) and does not have a leading blank space, but field2 and field3 are text fields defined as either text or varchar in the mysql database and these do have leading blank spaces.
Help would be greatly appreciated !!!
Thanks, PaulB