-2

I have following function to import subscriber list in database, this works fine when subscriber list is less than 500, but more than 500 record is there than browser show page is no available but all subscribers list get insert.

Following is my import code :

    function import_csv($csvfile,$databasetable,$uid) {
  $CI =& get_instance();
    $CI->session->unset_userdata('csv_import');

    if(!file_exists($csvfile)) {
        $CI->session->set_flashdata('error','File not found. Make sure you specified the correct path.\n');
        return false;
    }

    $file = fopen($csvfile,"r");

    if(!$file) {
        $CI->session->set_flashdata('error','Error opening data file.');
        return false;
    }

    $size = filesize($csvfile);

    if(!$size) {
        $CI->session->set_flashdata('error','File is empty.');
        return false;
    }

    $i=1;
    $queries="";
    $insert_count=0;
    while($row=fgetcsv($file)) {
    $row[]=date('Y-m-d h:i:s');
    $row[]='while list';
    $row[]=$uid;
        $linemysql = implode("','",$row);
    $linemysql="'".$linemysql."'";
        $query = "insert into ".$databasetable."
        (`name`, `lname`, `mobile`, `gender`, `dob`, `email`, `city`, `pin`, `address`, `marital_status`, `anniversary_date`,`added_on`,`status`,`uid`)
        values($linemysql);";

        $queries.=$query;
        if(count($row) == 14 && $row[0]!='' && strlen($row[2]) == 10 && is_numeric($row[2])) {
            if($CI->db->query($query)) {
                $insert_count++;
                        $value=$CI->session->userdata('csv_import');
                        if($value=='') {
                            $value=$CI->db->insert_id();
                        }
                        else {
                            $value.=','.$CI->db->insert_id();
                        }
                        $CI->session->set_userdata('csv_import',$value);
            }
        }
        $i++;
    }
    $CI->session->set_flashdata('success',$insert_count.' Subscriber added to your account');
    return true;
}
  • Ehm, are you sure what you're doing here? You're firing 500 queries to insert 500 rows... if you have 500 rows in your csv file... that's a bit overkill.. the website may give you the error because it takes too long to load. – Tosfera Dec 15 '14 at 11:58
  • so what is solution for import csv? – naran.arethiya Dec 15 '14 at 12:05
  • You should build up 1 massive query, and fire it at once. Let me give you an example. – Tosfera Dec 15 '14 at 12:05

1 Answers1

0
$query = "insert into ".$databasetable." (`name`, `lname`, `mobile`, `gender`, `dob`, `email`, `city`, `pin`, `address`, `marital_status`, `anniversary_date`,`added_on`,`status`,`uid`) VALUES ";
while ( $row = fgetcsv($file) ) {
    query = query . " ( ". $row [0] .", ". .. .", ". .. ." ) ";
}
$CI->db->query ( $query );

This'll create one massive query, I don't know your setup from the csv so I blanked out the fields with '..', you should add the right $row[] there yourself. After the loop is finished, it'll fire the query. Just make it suitable for your script. There was also a lot of rubbish code in there, make sure to clean it before using it on a live website ^^

Tosfera
  • 512
  • 3
  • 14