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