0

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

misterManSam
  • 24,303
  • 11
  • 69
  • 89
  • 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. – Rimble 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
  • @TomKriek.it is still showing the same array as displayed above.No success :( – user3489398 Aug 13 '14 at 15:26
  • then you've got EOL characters in that file that php's not understanding. e.g. loading the file on windows when it's a unix text file. – Marc B Aug 13 '14 at 16:18

1 Answers1

0

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";

}
Len_D
  • 1,422
  • 1
  • 12
  • 21