i quite new php , need issue. explain problem in following steps:
1- have csv file , import mysql database php.
2- when add commas @ end of each lines in csv file, import using import option in mysql db. can not import without adding commas @ end of lines.
3- want add new line php code add commas @ end of each record/line , enable data imported without error.
here code:
<?php //read file $csvfile=file_get_contents("/samba/import/tbl_hrlist.csv"); $lines = explode(php_eol, $csvfile); $array = array(); foreach ($lines $line) { $field = str_getcsv($line); if ($field[0] != ''){ $sql="insert hr_tbl (employee_id, first name, prefix, surname, location, organizational code, organizational unit, team, team code, function, function code, t24 department code, date in service (gbi), company email, end date contract, end date systems, temp. leave date, temp. leave end, temp. leave code) values ('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]','$field[9]','$field[10]','$field[11]','$field[12]','$field[13]','$field[14]','$field[15]','$field[16]','$field[17]','$field[18])"; } //insert record database if ($conn->query($sql) === true) { echo "new record created successfully". php_eol; } else { echo "error: " . $sql . "<br>" . $conn->error; } } else { continue; } } $conn->close(); ?> thanks in advance alex
use load data infile , completely abandon above idea.
you can have csv lines terminate \n or \r\n
you can have row 1 of import file either have or not have column names. if have column names, skip first row.
if have column names on row1, can have, say, 12 columns, , use 4 if want.
it flexible construct. check out.
https://dev.mysql.com/doc/refman/5.1/en/load-data.html
load data infile 'data.txt' table tbl_name fields terminated ',' enclosed '"' lines terminated '\r\n' ignore 1 lines;
Comments
Post a Comment