[et_pb_section bb_built=”1″][et_pb_row][et_pb_column type=”4_4″][et_pb_code _builder_version=”3.17.6″] csv2 sql

CSV to Mysql

This Php Script Will Import very large CSV files to MYSQL database in a minute

eg. MYDATA.csv

if(isset($_POST[‘username’])&&isset($_POST[‘mysql’])&&isset($_POST[‘db’])&&isset($_POST[‘username’])){$sqlname=$_POST[‘mysql’];$username=$_POST[‘username’];$table=$_POST[‘table’];if(isset($_POST[‘password’])){$password=$_POST[‘password’];}else{$password= ”;}$db=$_POST[‘db’];$file=$_POST[‘csv’];$cons= mysqli_connect(“$sqlname”, “$username”,”$password”,”$db”) or die(mysql_error());$result1=mysqli_query($cons,”select count(*) count from $table”);$r1=mysqli_fetch_array($result1);$count1=(int)$r1[‘count’];//If the fields in CSV are not seperated by comma(,) replace comma(,) in the below query with that delimiting character //If each tuple in CSV are not seperated by new line. replace \n in the below query the delimiting character which seperates two tuples in csv// for more information about the query http://dev.mysql.com/doc/refman/5.1/en/load-data.htmlmysqli_query($cons, ‘ LOAD DATA LOCAL INFILE “‘.$file.'” INTO TABLE ‘.$table.’ FIELDS TERMINATED by \’,\’ LINES TERMINATED BY \’\n\’‘)or die(mysql_error());$result2=mysqli_query($cons,”select count(*) count from $table”);$r2=mysqli_fetch_array($result2);$count2=(int)$r2[‘count’];$count=$count2-$count1;if($count>0)echo “Success”;echo “ total $count records have been added to the table $table “;}else{echo “Mysql Server address/Host name ,Username , Database name ,Table name , File name are the Mandatory Fields”;}?>


1. Keep this php file and Your csv file in one folder
2. Create a table in your mysql database to which you want to import
3. Open the php file from your localhost server
4. Enter all the fields
5. click on upload button

Facing Problems ? Some of the reasons can be the ones shown below

1) Check if the table to which you want to import is created and the datatype of each column matches with the data in csv
2) If fields in your csv are not separated by commas go to Line 117 of php file and change the query
3) If each tuple in your csv are not one below other(i.e not seperated by a new line) got line 117 of php file and change the query