Php imports a csv file to the mysql database ,. Php imports csv files to the mysql database. This document describes how php imports csv files to the mysql database. Share it with you for your reference. The specific analysis is as follows: how to import a csv file to the mysql database in this php program,
This example describes how php imports a csv file to a mysql database. Share it with you 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, and then save the data to the array through the fopen and fgetcsv files in php, then insert the data into the mysql database using the while statement. The code is as follows:
The code is as follows:
<? Php
$ Fname = $ _ files ['myfile'] ['name'];
$ Do = copy ($ _ files ['myfile'] ['tmp _ name'], $ fname );
If ($ do ){
Echo "data imported successfully
";
} Else {
Echo "";
}
Error_reporting (0); // import a csv file
$ Connect = mysql_connect ("localhost", "root", "") or die ("cocould 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, Y, er, 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" 1 second transfer to the list page, please wait .";
?>
I hope this article will help you with php programming.
Example: This article describes how php imports a csv file to a mysql database. Share it with you for your reference. The specific analysis is as follows: this process...