How to import CSV Data into MYSQL database using P

2019-06-09 18:51发布

Hi I have seen all over stack but couldn't find any appropriate answer for my question. The Answer were on how to read csv format and not to import into MYSQL Database.

I have a Upload Controller That uploads my file on my server. Now i want that uploaded file to be imported into MYSQL database. Please Help Me.

The Controller File:

public function upload_it() {
    //load the helper
    $this->load->helper('form');

    //Configure
    //set the path where the files uploaded will be copied. NOTE if using linux, set the folder to permission 777
    $config['upload_path'] = 'application/views/uploads/';

// set the filter image types
    $config['allowed_types'] = 'gif|csv';

    //load the upload library
    $this->load->library('upload', $config);

$this->upload->initialize($config);

$this->upload->set_allowed_types('*');

    $data['upload_data'] = '';

    //if not successful, set the error message
    if (!$this->upload->do_upload('userfile')) 
    {
        $data = array('msg' => $this->upload->display_errors());

    } 
    else
    { 

            //else, set the success message
            $data = array('msg' => "Upload success!");

            $data['upload_data'] = $this->upload->data();   

            if($_POST)
            {
                 if (($_FILES[csv][size] > 0 ) && ( $_FILES[csv][type]=="text/csv") ) 
                {
                        //get the csv file 
                        $file = $_FILES[csv][tmp_name]; 
                        $filetype=$_FILES[csv][type];
                        $handle = fopen($file, "r");
                        $i = 0;
                        $temp=str_getcsv($handle,"\n");
                        error_log("===$temp==");


                        $data = $this->user_m->('sip_id', 'sip_pass', 'name', 'key', 'email', 'password', 'phone', 'status', 'created', 'balance');


                        //Dont Know what to do next. 



                }

            }










    }

    //load the view/upload.php
    $this->load->view('admin/user/upload', $data);

}

Now I think should be creating a model that imports this uploaded file. But I dont know how to do that

Edit:

I know how to do this in php to make a connection to mysql:

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 

// Create connection 
$conn = new mysqli($servername, $username, $password); 

// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully"; 
?>

1条回答
smile是对你的礼貌
2楼-- · 2019-06-09 19:05

Ok, as you told me through chat that you can call a php file through exec, and CI raw sql is new to you (and trust me I don't know CI at all)....

And as you have stated that the file name is going to be known from, let's call it a php file with a form, and that the file has already been uploaded to the view folder in some known hierarchy, then consider the following string:

LOAD DATA INFILE '/full/path/to/view/myfile.txt' 
INTO TABLE users  
    FIELDS TERMINATED BY ',' 
           OPTIONALLY ENCLOSED BY '"'
    LINES  TERMINATED BY '\n'

Yes, that will be one big string in php. So it will be like any other string, like a select statement. After you connect with mysqli (as you showed me, and I edited the question), then execute it !

If the filename coming into the PHP $_POST needs to be concatenated into the blue block above, then that is what needs to happen. That single quote after the filename is critical, trust me.

From the Manual page for Load Data

查看更多
登录 后发表回答