Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-26
3
Medium Priority
?
1,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

704 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