如何使用PHP CodeIgniter将CSV数据导入MYSQL数据库? [英] How to import CSV Data into MYSQL database using PHP CodeIgniter?

查看:297
本文介绍了如何使用PHP CodeIgniter将CSV数据导入MYSQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我看到所有的堆栈,但找不到任何适当的答案,我的问题。答案是关于如何读取csv格式,而不是导入到MYSQL数据库。

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.

我有一个上传控制器,上传我的文件在我的服务器上。现在我想把上传的文件导入到MYSQL数据库。请帮助我。

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.

控制器文件:

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

我知道如何在php到mysql的连接:

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


推荐答案

好的,正如你通过聊天告诉我,通过exec调用php文件,CI raw sql对你来说是新的(并且相信我根本不知道CI

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)....

正如你所说的那样,文件名将被知道,让我们称之为一个带有表单的php文件,并且该文件已经上传到视图文件夹在一些已知的层次结构,然后考虑以下字符串:

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'

是的,这将是php中的一个大字符串,因此它将像任何其他字符串,如select语句。在您连接 mysqli (如您所示,我编辑了问题),然后执行它!

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 !

如果文件名进入PHP $ _ POST 需要连接到上面的蓝色块,那就是需要发生的。那个单引号后的文件名是至关重要的,相信我。

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.

加载数据

这篇关于如何使用PHP CodeIgniter将CSV数据导入MYSQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆