Php imports csv files to mysql database _ PHP Tutorial

Source: Internet
Author: User
Tags mysql tutorial
Php imports the csv file to the mysql database. Php imports a csv file to the mysql database * 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, however, php imports the csv file to the mysql database/* the principle of data import in this program is to first upload the csv file to the server, then, use the fopen and fgetcsv files in php to save the data to the array, and then use the while command to insert the data into the mysql database.

Php Tutorial import csv file to mysql tutorial database tutorial
/*
In this program, the principle of data import 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, and then insert the data into the mysql database using while.
*/

$ 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 ."

?>

Http://www.bkjia.com/PHPjc/630837.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630837.htmlTechArticlephp to the csv file into mysql database/* the program to achieve data import principle is to first upload the csv file to the server, and then through the php fopen and fgetcsv file to save the data to the array, however...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.