A detailed explanation of how PHP implements the CSV file import database

Source: Internet
Author: User
Tags php programming import database
How does PHP implement CSV file Import database? This article mainly introduces the PHP programming implementation of CSV file import MySQL database method, involving PHP file reading, conversion, database connection, insert and other related operations skills, need to refer to friends. We hope to help you.

Specific as follows:

config.db.php content is as follows;

<?php$username= "root"; $userpass = "123"; $dbhost = "localhost"; $dbdatabase = "credits2stakes";//Generate a connection $db_connect= Mysql_connect ($dbhost, $username, $userpass) or die ("Unable to connect to the mysql!"); /Select a database to operate on mysql_select_db ($dbdatabase, $db _connect);

index.php content is as follows:

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/><form name=" Frm1 "enctype=" Multipart/form-data "action=" insertdb.php "method=" POST ">  <input name= "filename" type= "file"/><input name= "Submit" type= "Submit" value= "Import"/></form>

insertdb.php content is as follows:

<?phpsession_start (); Header ("Content-type:text/html;charset:utf-8");//global variable $file=$_files[' filename ']; $max _ Size= "2000000"; Maximum file limit (in bytes) $fname = $file [' name ']; $ftype =strtolower (substr (STRRCHR ($fname, '. '), 1));//file Format $uploadfile= $file [' Tmp_name '];if ($_server[' request_method ']== ' POST ') {if (Is_uploaded_file ($uploadfile)) {if ($file [' Size ']> $max     _size) {echo "Import file is too large";     Exit     } if ($ftype! = ' csv ') {echo "Import file type is error";     Exit   }}else{echo "The file is not empty!";   Exit  }}require ("./config.db.php"); Connect MySQL database $row=0; $filename = $file [' Tmp_name ']; $handle =fopen ($filename, ' R '); while (!feof ($handle) && $  Data=fgetcsv ($handle, +, ', ')) {$arr _result=array ();   if ($row ==0) {$row + +;  Continue    } if ($row >0 &&!empty ($data)) {$num =count ($data);   for ($i =0; $i < $num; $i + +) {Array_push ($arr _result, $data [$i]);  }//$name = Iconv (' gb2312 ', ' utf-8 ', $arr _result[1]); $sex = Iconv (' gb2312 ', ' Utf-8 ', $arr _RESULT[2]);  $sql = "INSERT into Inviter (inviter,invitees,time) value ($arr _result[1], $arr _result[2], $arr _result[3])";  Echo $sql;  mysql_query ("Set names UTF8");  $result =mysql_query ($sql); if ($result) {echo "Insert succeeded!!!   "; }else{echo "insertion failure!!!      "; }} $row + +;} Fclose ($handle);? >

Related recommendations:

A detailed explanation of how PHP prevents Thunder download method

PHP exports a CSV instance with a leading 0 share

About PHP parsing CSV data and outputting

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.