Home  >  Article  >  php教程  >  php实现连接access数据库并转txt写入的方法_php技巧

php实现连接access数据库并转txt写入的方法_php技巧

PHP中文网
PHP中文网Original
2016-05-25 17:07:02993browse

这篇文章主要介绍了php实现连接access数据库并转txt写入的方法,涉及php连接、读取access数据库及写入txt文件的相关操作技巧,需要的朋友可以参考下

本文实例讲述了php实现连接access数据库并转txt写入的方法。分享给大家供大家参考,具体如下:

这里的代码实现PHP读取手机归属地 并导入txt文件的功能(文章末尾附手机归属地 数据库)

mdbtotxt.php代码:

<?php
//php连接access测试
define(&#39;TABLE&#39;, &#39;shoujiguishudi&#39;);
define(&#39;OUTPUTFILE&#39;, &#39;output.txt&#39;);
define(&#39;ROOT&#39;,str_replace($_SERVER[&#39;PHP_SELF&#39;],&#39;&#39;,str_replace(&#39;\\&#39;,&#39;/&#39;,__FILE__)));
$conn = new com(&#39;adodb.connection&#39;);
$conn->Provider= &#39;microsoft.jet.oledb.4.0&#39;;
$conn->Open(ROOT.&#39;\\callerloc.mdb&#39;);//文件地址
$rs = new com(&#39;adodb.recordset&#39;);
$rs->Open(&#39;SELECT * FROM &#39;.TABLE, $conn, 1, 1);//查表
$i = 0;
$page=1;
if(!empty($_GET[&#39;page&#39;])){
  $page=$_GET[&#39;page&#39;];
  if($page<1)$page=1;
  if($page>$rs->PageCount)$page=$rs->PageCount;
}
$rs->AbsolutePage=$page;
//打开文件
$fp = fopen(OUTPUTFILE, "w");
echo &#39;<table width="100%"><tr>&#39;;
foreach( $rs->Fields as $name){
  echo &#39;<td>&#39;.$name->Name.&#39;</td>&#39;;
  fwrite($fp,$name->Name."\t");
}
fwrite($fp,"\r\n");
echo &#39;</tr>&#39;;
for(;!$rs->EOF;$rs->MoveNext()){
  echo &#39;<tr>&#39;;
  foreach( $rs->Fields as $name){
    //echo &#39;<td>&#39;.$name.&#39;</td>&#39;;
    fwrite($fp,$name."\t");
  }
  $i++;
  fwrite($fp,"\r\n");
  if($i%1000==0)
    echo $i."<br>";
  echo &#39;</tr>&#39;;
  if($rs->AbsolutePosition>=$rs->AbsolutePage*$rs->PageSize)
    break;
}
echo &#39;</tale>&#39;;
fclose($fp);
?>


附:本例中的access数据库

Statement:
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