Tip sharing PHP batch export csv files_PHP tutorial

WBOY
Release: 2016-07-15 13:33:14
Original
757 people have browsed it

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:

  1. < ?php
  2. function getmicrotime(){
  3. list($usec, $sec ) = explode(” “,microtime());
  4. return ((float)$usec + (float)$sec);
  5. }
  6. ?>

<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(&rdquo;db.inc.php&rdquo;);//连接数据库  </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> (&rdquo;test.csv&rdquo;,&rdquo;r&rdquo;);  </span></li><li><span>$</span><span class="attribute">sql</span><span>=&rdquo;insert into scores(idcard,names<br />,num,sex,nation,score) values(&rsquo;&rdquo;;  </span></li><li class="alt"><span>while ($</span><span class="attribute">data</span><span> = </span><span class="attribute-value">fgetcsv</span><span> ($handle, 1000, &ldquo;,&rdquo;)) {  </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].&rdquo;&lsquo;)&rdquo;;break;}  </span></li><li class="alt"><span>$</span><span class="attribute">sql</span><span>=$sql.$data[$c].&rdquo;&lsquo;,&rsquo;&rdquo;;  </span></li><li><span>}  </span></li><li class="alt"><span>print &ldquo;</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>
Copy after login

I hope you can master this technique through the above code example of batch exporting csv files in PHP.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446090.htmlTechArticleSometimes when writing a program, the background requires importing a large amount of data into the database, such as querying computer test scores and phone books. Data, etc. are generally stored in excel. At this time we can...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template