Batch import of php Data into csv files (example)
/** Import csv files to the database in php ***
- * Calculate the program execution time simultaneously.
- ***/
- // 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, 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 execution successful!
";
- $ 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 ";
- ?>
|