Php method for importing CSV files into MySQL database, _php tutorial

Source: Internet
Author: User

PHP to import the CSV file into the MySQL database method,


This example describes how PHP imports a CSV file into a MySQL database. Share to everyone for your reference. The specific analysis is as follows:

This program to implement data import principle is to upload the CSV file to the server, and then through the PHP fopen and fgetcsv files to save the data to an array, and then use while to insert data into the MySQL database, the code is as follows:
Copy CodeThe code is as follows:
<?php
$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 " 1 seconds to the list page, please wait a moment. ";
?>

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/932492.html www.bkjia.com true http://www.bkjia.com/PHPjc/932492.html techarticle PHP imports the CSV file into the MySQL database method, this article describes the PHP import CSV file into the MySQL database method. Share to everyone for your reference. The specific analysis is as follows: This process ...

  • 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.