+ 4

How to import csv file with ; separator in mysql via php, i can import csv with coma but not with semicilon.

How to import csv file with ; separator in mysql via php, i can import csv with coma but not with semicilon. ///////////////////////////////////////////////////// Code ///////////////////////////////////////////////////// include_once("db_connect.php"); if(isset($_POST['import_data'])){ // validate to check uploaded file is a valid csv file $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'); if(!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'],$file_mimes)){ if(is_uploaded_file($_FILES['file']['tmp_name'])){ $csv_file = fopen($_FILES['file']['tmp_name'], 'r'); //fgetcsv($csv_file); // get data records from csv file while(($emp_record = fgetcsv($csv_file)) !== FALSE){ // Check if employee already exists with same email $sql_query = "SELECT login, pass FROM user WHERE login = '".$emp_record[0]."'"; $resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn)); // if employee already exist then update details otherwise insert new record if(mysqli_num_rows($resultset)) { $sql_update = "UPDATE user set login='".$emp_record[0]."', pass='".$emp_record[1]."' WHERE login = '".$emp_record[0]."'"; mysqli_query($conn, $sql_update) or die("database error:". mysqli_error($conn)); } else{ $mysql_insert = "INSERT INTO user (login, pass )VALUES('".$emp_record[0]."', '".$emp_record[1]."')"; mysqli_query($conn, $mysql_insert) or die("database error:". mysqli_error($conn)); } } fclose($csv_file); $import_status = '?import_status=success'; } else { $import_status = '?import_status=error'; } } else { $import_status = '?import_status=invalid_file'; } } header("Location: index.php".$import_status); //////////////////////////////////////////////////////////////////////////////////////// csv files : NAME;PASSWORD

23rd Oct 2017, 9:42 AM
sfeuh
sfeuh - avatar
4 Answers
+ 6
Hi I find the solution while(($emp_record = fgetcsv($csv_file,10000, ";")) Thank you all.
23rd Oct 2017, 10:21 AM
sfeuh
sfeuh - avatar
+ 5
thank You daniel, i will see the link.
22nd Oct 2017, 7:28 PM
sfeuh
sfeuh - avatar
+ 3
Hi @Daniel FernĂĄndez Hidalgo Teh first link it's for the coma separator. The second link the solution not work. Thank you.
23rd Oct 2017, 9:44 AM
sfeuh
sfeuh - avatar