• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • Last Modified:

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

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
skylabel
Asked:
skylabel
1 Solution
 
skylabelAuthor Commented:
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
 
hernst42Commented:
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
 
RoonaanCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now