Home > Backend Development > PHP Tutorial > How to import csv files into mysql database in php_PHP tutorial

How to import csv files into mysql database in php_PHP tutorial

WBOY
Release: 2016-07-13 10:10:50
Original
692 people have browsed it

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.";
?>
" method="post">

Import cvs data


I hope this article will be helpful to everyone’s PHP programming design.

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template