Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

line break problem importing and exporting csv file in/out of database.........

Posted on 2004-08-26
3
Medium Priority
?
1,040 Views
Last Modified: 2008-01-09
Hi, I've got the following script to import data into the database from a textfile....

<?php
include 'connectdb.php'; $countrow=0;
$n=0;
$array = file("import_members.csv");
foreach($array as $row) {
 $record = explode(",",$row);

$sub=$record[0]; $id=$record[1];  $gamertag=$record[2]; $firstname=$record[3]; $lastname=$record[4];$email=$record[5];

   mysql_query("INSERT INTO members (mem_sub, mem_id, mem_gamertag, mem_firstname, mem_lastname, mem_email) VALUES('$sub','$id','$gamertag','$firstname','$lastname','$email')")
   or die (mysql_error());
   $n=$n+1;
}
echo $n." Records were imported to members database successfully......";
?>

And this is how I export data from the databse to another csv file.....

<?
$n=0;
//Connect to database
include 'connectdb.php';

$result=mysql_query("SELECT * FROM members");
$fp = fopen( "exceldata/members.csv" , "w" );
//select all records
while ($row = mysql_fetch_array($result))
{
 $n=$n+1;
 $inputString=$row['mem_email']."\n";
  fwrite( $fp, $inputString );
}
 echo "<br> members.csv updated";
fclose( $fp );
?>

Now the problem is that I'm getting double line breaks in my csv file.....I'm trying to figure out what causes this and I'm confused...Here are a few of observations.......
If I manually enter/modify records, using a form or phpMyAdmin, the output file is fine....
If I open the outpu file with notepad, instead of excl or wordpad, all the rows are on one line...

been fooling around for hours but Just can't  seem to make it work the way i want.........






0
Comment
Question by:skylabel
3 Comments
 
LVL 1

Author Comment

by:skylabel
ID: 11910231
the output file is fine when i omit the ."\n" linebreak
but then if add records manually on top of that, it stays on the same line.....so my output fie would be something like (just to clarify what i mean the first 3 records were imported while the 4h was manually added)

1, john, doe, blaba
2, rank, franky, blala
3, bill, williams, blabla, 4, jane, jameson, blabla
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 11910249
The problem comes from $array = file("import_members.csv");
the lines in that array contain the \n at the end of the last entry

So Try this line
$record = explode(",",trim($row));
for
$record = explode(",",$row);

Trim removes the whitespaces at the end and the beginning line.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 11910284
When you just want to remove the linebreaks at the end of the last element, you can use:



I add this comment because I do not know if your first field might or might not contain leading spaces (or your last field the same with trailing spaces/tabs), which will surely be deleted when using trim().
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question