#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    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)
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Erm, the output is what i typed earlier in the thread..
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    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:
    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.
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo