Sometimes, when writing a program, the background requests to import a large amount of data into the database. for example, the computer test result query and phone book data are generally stored in excel, in this case, we can export the data to a csv file, and then use the following program
Sometimes, when writing a program, the background requests to import a large amount of data into the database. for example, the computer test result query and phone book data are generally stored in excel, in this case, we can export the data to a csv file, and then use the following program to batch import the data to the database in the background.
The following is only an important part of the program:
***********
* ********* Www.webjx.com ***********
****************************************/
// Define a time function
Function getmicrotime (){
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ usec (float) $ sec );
}
?>
$ Time_start = getmicrotime ();
Include ('Db. inc. php'); // connect to the database
$ Db = new testcsv;
?>
$ Handle = fopen ('test.csv ', 'r ');
$ SQL = 'Insert into scores (idcard, names, num, ***, 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, ***, nation, score) values ('';
}
Fclose ($ handle );
$ Time_end = getmicrotime ();
$ Time = $ time_end-$ time_start;
Echo 'Program Execution time: '. $ time.' second ';
?>