The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - CSV Line Break problem
Discuss CSV Line Break problem in the PHP Development forum on Dev Shed. CSV Line Break problem PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 7th, 2012, 10:09 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 16 m 50 sec
Reputation Power: 0
|
|
|
PHP-General - CSV Line Break problem
I have this CSV file that contain this following,
10000,hello
10000,bye
10000,good
And, my code below for reading CSV file, and send them to database.
PHP Code:
if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
print_r($data);
mysql_query("INSERT INTO table_name (table_column1, table_column2) VALUES
(
'".$data[0]."',
'".$data[1]. "'
)
");
}
} while ($data = fgetcsv($handle,1000,","));
}
With the above, the data from CSV cannot go into database because, it read the file something like this below. The echoed data:
Array ( [0] => 10000 [1] => hello 10000 [2] => bye 10000 [3] => good)
May i know, what the is problem with this? Is there something wrong with the codes?
However, if the format for my CSV file is like this below (A blank line in between each line),
10000,hello
10000,bye
10000,good
All the data can go into database successsfully. The echo data:
Array ( [0] => 10000 [1] => hello)
Array ( [0] => 10000 [1] => bye)
Array ( [0] => 10000 [1] => good)
|

November 7th, 2012, 10:22 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Your code is slightly wrong (you're using a do-while loop instead of a regular while loop) and your output doesn't match the code. Show us what happens when you echo these SQL queries.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

November 7th, 2012, 11:00 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 16 m 50 sec
Reputation Power: 0
|
|
|
Erm, the output is what i typed earlier in the thread..
|

November 8th, 2012, 08:20 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Ok, respond to only 1/3 of what I said, that's how we do it around here.
Your file doesn't have newlines, or has the wrong kind of newlines. It's splitting your "line" on comma, but it's considering the file as all one line.
From the manual:
Quote: Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. |
|

November 8th, 2012, 10:38 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 16 m 50 sec
Reputation Power: 0
|
|
|
Hi ManiacDan,
Thanks for your replies, and i managed to solve it..
Coz i was using Mac, the excel conversion to csv is very different from Windows. I was comparing two csv on notepad, and realised the mac csv format is quite different on notepad. So i tried to convert on Windows excel and upload to database without blank line in between. And it is able to go into database!
So by the way, thanks for the help/thoughts.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|