How to import csv files into mysql database in php,
The example in this article describes how to import csv files into mysql database in php. Share it with everyone for your reference. The specific analysis is as follows:
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. The code is as follows:
Copy code The code is as follows:
$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 take 1 second to transfer to the list page, please wait.";
?>
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/932492.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932492.htmlTechArticleHow to import csv files into mysql database in php. This article describes how to import csv files into mysql database in php. method. Share it with everyone for your reference. The specific analysis is as follows: This process...