Home >Backend Development >PHP Tutorial >Tip sharing PHP batch export csv files_PHP tutorial
For
Sometimes when writing a program, the background requires importing a large amount of data into the database. For example, querying computer test scores, phone book data, etc. are generally stored in excel , then we can export the data into a csv file, and then use the following procedure to batch import the data into the database in the background.
The following is just the main program part for batch exporting csv files with PHP:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>$</span><span class="attribute">time_start</span><span> = </span><span class="attribute-value">getmicrotime</span><span>(); </span></li><li class="alt"><span>include(”db.inc.php”);//连接数据库 </span></li><li><span>$</span><span class="attribute">db</span><span>=</span><span class="attribute-value">new</span><span> testcsv; </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li> <li> <span class="tag"><</span><span> ?php </span></li><li class="alt"><span>$</span><span class="attribute">handle</span><span> = </span><span class="attribute-value">fopen</span><span> (”test.csv”,”r”); </span></li><li><span>$</span><span class="attribute">sql</span><span>=”insert into scores(idcard,names<br />,num,sex,nation,score) values(’”; </span></li><li class="alt"><span>while ($</span><span class="attribute">data</span><span> = </span><span class="attribute-value">fgetcsv</span><span> ($handle, 1000, “,”)) { </span></li><li><span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">count</span><span> ($data); </span></li><li class="alt"><span>for ($</span><span class="attribute">c</span><span>=</span><span class="attribute-value">0</span><span>; $c </span><span class="tag"><</span><span> $num; $c++) { </span></li><li><span>if($</span><span class="attribute">c</span><span>==$num-1){$</span><span class="attribute">sql</span><span>=$sql.$data[$c].”‘)”;break;} </span></li><li class="alt"><span>$</span><span class="attribute">sql</span><span>=$sql.$data[$c].”‘,’”; </span></li><li><span>} </span></li><li class="alt"><span>print “</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”; </span> </li> <li> <span>echo $sql.”</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”; </span> </li> <li class="alt"> <span>$db-</span><span class="tag">></span><span>query($sql); </span> </li> <li> <span>echo “SQL语句执行成功!</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span class="tag">></span><span>”; </span> </li> <li class="alt"> <span>$</span><span class="attribute">sql</span><span>=”insert into scores(idcard,names<br>,num,sex,nation,score) values(’”; </span> </li> <li><span>} </span></li> <li class="alt"><span>fclose ($handle); </span></li> <li> <span>$</span><span class="attribute">time_end</span><span> = </span><span class="attribute-value">getmicrotime</span><span>(); </span> </li> <li class="alt"> <span>$</span><span class="attribute">time</span><span> = $time_end – $time_start; </span> </li> <li><span>echo “程序执行时间:”.$time.”秒”; </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
I hope you can master this technique through the above code example of batch exporting csv files in PHP.