Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I ma struggling to load information from a csv file using PHP and want to save the contents of csv (mobile phone numbers) into mysql database table.The file contents look like this:

CSV file contents ( individual record per single line with no commas)

44762126064    
447508751    
4474669756    
44771466603    
444584871    
445574805    
447455471039    
44777451345
447460345819    
44793342963    
44734838672    
44752845528    
4474537291    
44779645078

I am try to upload csv file using form and submit.The code read csv file and tries to write the content into mysql table.The problem is that code is returning all the csv records in one array element like this:

Array( 
    [0] => 44762126064 447508751 4474669756 44771466603 444584871 445574805 447455471039 44777451345 447460345819 44793342963 44734838672 44752845528 4474537291 44779645078 
);

and inserting the whole array as one record rather one mobile number per row in mysql table.

The code :

if (isset($_POST['submit'])) {
    if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
        echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
    echo "<h2>Displaying contents:</h2>";
    #    readfile($_FILES['filename']['tmp_name']);
}

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

           $import="INSERT into mobilecsv(phoneMobile,status) values('$data[0]',0)";

    mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

}

I have tried explode(), array_slice() etc but none of them helped me to split this array contents to save them as individual records/phone numbers in mysql database.

How can i split individual contents of first array element(as in my case) to save them as separate individual records as appearing in my CSV? I am a new programmer and would appreciate your help in this regard.Thanks

share|improve this question
    
do you tried explode(' ', $data[0]) ? –  Webice Aug 13 '14 at 15:10
2  
If your file is really just a single value per line, then there's no need for the fgetcsv() business. just do $lines = file('whatever.csv'); foreach($lines as $line) { insert into db } –  Marc B Aug 13 '14 at 15:11
    
@Webice yes i tried but it is not working –  user3489398 Aug 13 '14 at 15:19
    
The $data array is being refilled with new data, the same with a fetch request from MySQL. You can't echo back the results of all the lines of the fgetcsv function unless you store each found line into a new array. If you add $lines = array(); Somewhere before the while loop. And add this $lines[] = $data[0]; inside the while loop. You can then print_r() or var_dump the $lines array after the while loop all the phone numbers in a proper array. –  Tom Kriek Aug 13 '14 at 15:20
    
@MarcB not working because the value resides in a single array element :) –  user3489398 Aug 13 '14 at 15:23

1 Answer 1

up vote 0 down vote accepted

The problem is fgetcsv isn't detecting a comma which is at the heart of a COMMA separated value (CSV) file. Add this line at the top of your file after the php opening tag:

ini_set("auto_detect_line_endings", true);

change your import code to:

//Import uploaded file to Database
$handle = fopen($_FILES['filename']['tmp_name'], "r");
while(($data = fgetcsv($handle)) !== FALSE){
$phoneMobile = $line[0];

$import="INSERT into mobilecsv(phoneMobile,status) values('$phoneMobile',0)";
mysql_query($import) or die(mysql_error());
}

fclose($handle);

print "Import done";

}
share|improve this answer
    
Square bracket next to $phoneMobile –  Tom Kriek Aug 13 '14 at 15:25
1  
Thanks @TomKriek –  Len_D Aug 13 '14 at 15:37
    
Thank you Len_D.That one worked for me.Just need minor edits.Brilliant :) –  user3489398 Aug 13 '14 at 15:37
    
That's great@user3489398. Please accept my answer. –  Len_D Aug 13 '14 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.