PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 7th, 2012, 10:09 PM
gxdesmondgx gxdesmondgx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 gxdesmondgx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #2  
Old November 7th, 2012, 10:22 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,804 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 28 m 53 sec
Reputation Power: 6112
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.

Reply With Quote
  #3  
Old November 7th, 2012, 11:00 PM
gxdesmondgx gxdesmondgx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 gxdesmondgx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 50 sec
Reputation Power: 0
Erm, the output is what i typed earlier in the thread..

Reply With Quote
  #4  
Old November 8th, 2012, 08:20 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,804 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 17 h 28 m 53 sec
Reputation Power: 6112
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.

Reply With Quote
  #5  
Old November 8th, 2012, 10:38 PM
gxdesmondgx gxdesmondgx is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 gxdesmondgx User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - CSV Line Break problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap