/    Sign up×
Community /Pin to ProfileBookmark

CSV download file with leading space in some fields

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”;
}
[/code]

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@SodbusterJan 30.2009 — Spaces are echoed because you have spaces in your echo string. Try[code=php]echo "$field1,$field2,$field3n";[/code]or go with fputcsv();
Copy linkTweet thisAlerts:
@paulbauthorJan 30.2009 — Thanks Sodbuster - some of the most brilliant solutions are the simplest. I removed the blanks and it worked like a dream !!

PaulB
Copy linkTweet thisAlerts:
@SyCoJan 31.2009 — You can save few lines of code if you don't rename the variables for no reason too. In more complex scripts it can only lead to confusion and serves no purpose.

[code=php]while ($myrow = mysql_fetch_array($result)) {

echo $myrow['field1'].','.$myrow['field2'].','.$myrow['field3'];
} [/code]
×

Success!

Help @paulb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...