When writing a program when the background requirements to import a large number of data into the database, such as computer test results of the query, phone book data are generally stored in Excel, when we can export data into a CSV file, and then through the following program can be in the background batch import data into the database.
The following are just the main program sections:
<?php
/*****************************************
Author: Chong Xing/arcow****************
njj@nuc.edu.cn*******************
PHP import CSV file to database **********
Simultaneous calculation of program execution time ***********
www.111cn.net***********
****************************************/
Define Get Time function
function Getmicrotime () {
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}
?>
<?php
$time _start = Getmicrotime ();
include ("db.inc.php");//Connect database
$db =new testcsv;
<?php
$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 "<br>"; The
Echo $sql. <br> ";
$db->query ($sql);
Echo SQL statement executed successfully! <br> ";
$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 ";
?