For
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 is the main program part of PHP bulk export CSV file:
- < ? PHP
- function Getmicrotime () {
- List ($usec, $sec) = Explode ("", Microtime ());
- return (float) $usec + (float) $sec);
- }
- ?>
- < ? PHP
- $ Time_start = Getmicrotime ();
- Include ("db.inc.php");//Connect to 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, +, ",")) {
- $ Num = Count ($data);
- For ($c=0; $c < $num; $c + +) {
- if ($c= = $num-1) {$sql= $sql. $data [$c]. "')"; break;}
- $ SQL = $sql. $data [$c]. "', '";
- }
- print " < br>";
- 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 ";
- ?>
I hope you can master this technique by using the code sample of PHP batch export CSV file above.
http://www.bkjia.com/PHPjc/446090.html www.bkjia.com true http://www.bkjia.com/PHPjc/446090.html techarticle for sometimes writing programs 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 ...