Solved

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

Posted on 2004-08-26
3
1,032 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 500 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now