Sometimes write a program when the background requirements to import large amounts of data into the database, such as computer test results of the query, phone book data, etc. are generally stored in Excel, then we can export data into a CSV file, and then through the following procedures can be in the background batch import data into the database.
The following are just the main program sections:
The code is as follows |
Copy Code |
/***************************************** Author: Chong Xing/arcow**************** njj@nuc.edu.cn******************* PHP import CSV file to database ********** Simultaneous calculation of program execution time *********** www.knowsky.com*********** ****************************************/ Defining the Get Time function function Getmicrotime () { List ($usec, $sec) = Explode ("", Microtime ()); return (float) $usec + (float) $sec); } ?> $time _start = Getmicrotime (); Include ("db.inc.php");//Connect to Database $db =new testcsv; ?> $handle = fopen ("Test.csv", "R"); $sql = "INSERT into scores (Idcard,names,num,sex,nation,score) values ('"; while ($data = Fgetcsv ($handle, 1000, ",")) { $num = count ($data); for ($c =0; $c < $num; $c + +) { if ($c = = $num-1) {$sql = $sql. $data [$c]. "')"; break;} $sql = $sql. $data [$c]. "', '"; } Print " "; echo $sql. " "; $db->query ($sql); echo "SQL statement executed successfully! "; $sql = "INSERT into scores (Idcard,names,num,sex,nation,score) values ('"; } Fclose ($handle); $time _end = Getmicrotime (); $time = $time _end-$time _start; echo "Program execution Time:". $time. " Seconds "; ?>
|
http://www.bkjia.com/PHPjc/630709.html www.bkjia.com true http://www.bkjia.com/PHPjc/630709.html techarticle sometimes write a program when the background requires a large amount of data into the database, such as computer test results of the query, phone book data, etc. are generally stored in Excel, then we can put the data ...