Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

csv file import to mysqli using php

<?php

   if(!empty($_FILES['file']['name']))
   {
    $connect = new PDO("mysql:host=localhost;dbname=dn_tcs_project_data;", "root", "", array(
           PDO::MYSQL_ATTR_LOCAL_INFILE => true,
       ));
   $mysqli = new mysqli("localhost","root","","dn_tcs_project_data");
    $total_row = count(file($_FILES['file']['tmp_name']));
   
    $file_location = str_replace("", "/", $_FILES['file']['tmp_name']);
   
    $query_1 = '
    LOAD DATA LOCAL INFILE "'.$file_location.'" IGNORE 
    INTO TABLE tcs_raw_task_sheets 
    FIELDS TERMINATED BY "," 
    LINES TERMINATED BY "
" 
    IGNORE 1 LINES 
    (@column1,@column2,@column3,@column4,@column5,@column6,@column7,@column8,@column9) 
    SET MW = @column1, HT_MW = @column2,  D_MW = @column3, C_MW = @column4, QA = @column5, D_QA = @column6, C_QA = @column7, R_QA = @column8, R_EVA = @column9
    ';
   
    $statement = $connect->prepare($query_1);
   
    $statement->execute();
    if ($statement) {
          $output = array(
     'success' => 'Total <b>'.$total_row.'</b> Data imported'
    );
   
    echo json_encode($output);
?>
 
PREVIOUS NEXT
Tagged: #csv #file #import #mysqli #php
ADD COMMENT
Topic
Name
3+4 =