Last active
August 29, 2015 14:05
-
-
Save maisnamraju/ca03a90be0c7dc6e6c23 to your computer and use it in GitHub Desktop.
Uploading csv data to mysql database in codeigniter with Timestamp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$file_path = FCPATH.'/uploads/'. $current_user.'.csv'; //It should always be set to FCPATH to make sure that the path is absolute | |
$query_name = "LOAD DATA LOCAL INFILE '" | |
. $file_path . | |
"' INTO TABLE `" | |
. $this->table_name . | |
"` FIELDS TERMINATED BY ',' | |
LINES TERMINATED BY '\\n' | |
IGNORE 1 LINES | |
(time_stamp,curr_property,curr_property_cost,day_property,day_property_cost,curr_solar_generating,curr_solar_export,day_solar_generated,day_solar_export,curr_chan1,curr_chan2,curr_chan3,day_chan1,day_chan2,day_chan3) | |
"; | |
if (!$this->db->table_exists($this->table_name)) { | |
$fields = array( | |
'id' => array( | |
'type' => 'INT', | |
'constraint' => 10, | |
'unsigned' => TRUE, | |
'auto_increment' => TRUE | |
), | |
'time_stamp' => array( | |
'type' => 'VARCHAR', | |
'constraint' => 30, | |
'null' => FALSE | |
), | |
'curr_property' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_property_cost' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_property' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_property_cost' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_solar_generating' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_solar_export' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_solar_generated' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_solar_export' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_chan1' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_chan2' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'curr_chan3' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_chan1' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_chan2' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'day_chan3' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
), | |
'total_phase' => array( | |
'type' => 'INT', | |
'constraint' => 5, | |
'unsigned' => TRUE | |
) | |
); | |
$this->dbforge->add_field($fields); | |
$this->dbforge->add_key('id', TRUE); | |
$this->dbforge->create_table($this->table_name); | |
$link = mysqli_init(); | |
if (!$link) { | |
die('mysqli_init failed'); | |
} | |
if (!mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true)) { | |
die('Setting MYSQLI_OPT_LOCAL_INFILE failed'); | |
} | |
if (!mysqli_real_connect($link, 'localhost', 'root', 'password', 'dashboard')) { | |
die('Connect Error (' . mysqli_connect_errno() . ') ' | |
. mysqli_connect_error()); | |
} | |
$result = $link->query($query_name); | |
if (!$result) { | |
printf("%s\n", mysqli_error($link)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment