Home >Backend Development >PHP Tutorial >php import csv file into mysql database_PHP tutorial
php import csv file into mysql database /* The principle of data import in this program is to first upload the csv file to the server, then save the data into an array through php's fopen and fgetcsv files, and then use while to insert the data into the mysql database one by one
php tutorial to convert csv files Import to mysql tutorial database tutorial
/*
The principle of data import in this program is to first upload the csv file to the server, then save the data to the array through php's fopen and fgetcsv files, and then use while to insert the data into the mysql database one by one
*/
$fname = $_files['myfile']['name'];
$do = copy($_files['myfile']['tmp_name'],$fname);
if ($do){
echo "Import data successfully
";
}else{
echo "";
}
error_reporting(0);//Import files in csv format
$connect=mysql_connect("localhost","root","") or die("could not connect to database");
mysql_select_db("gklqtzcx",$connect) or die (mysql_error());
mysql_query("set names 'gbk'");
$fname = $_files['myfile']['name'];
$handle=fopen("$fname","r");
while($data=fgetcsv($handle,10000,",")){
$q="insert into records (name,classes,a_time,college,notify,receiver,r_time,handler) values ('$data[1]','$data[2]','$data[3]', '$data[4]','$data[5]','$data[6]','$data[7]','$data[8]')";
mysql_query($q) or die (mysql_error());
}
fclose($handle);
echo "It will transfer to the list page in 1 second, please wait."?>