Heim >Backend-Entwicklung >PHP-Tutorial >技巧分享 PHP批量导出csv文件_PHP教程
对于
有时写程序时后台要求把大量数据导入数据库中,比如计算机考试成绩的查询、电话簿的数据等一般都是存放在excel中的,这时我们可把数据导出成csv文件,然后通过以下程序即可在后台批量导入数据到数据库中。
下面只是PHP批量导出csv文件的主要程序部分:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></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> </li> <li><span class="tag"><span> ?php </span></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> $num; $c++) { </span></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 class="tag-name">br</span><span class="tag">></span><span>”; </span></span> </li> <li> <span>echo $sql.”</span><span class="tag"><span> </span><span class="tag-name">br</span><span class="tag">></span><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 class="tag-name">br</span><span class="tag">></span><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>
希望大家通过以上PHP批量导出csv文件的代码示例能够掌握这一技巧。