php实现连接access数据库并转txt写入的方法
这里的代码实现PHP读取手机归属地 并导入txt文件的功能(文章末尾附手机归属地 数据库)
mdbtotxt.php代码:
- <?php
- //php连接access测试
- define('TABLE','shoujiguishudi');
- define('OUTPUTFILE','output.txt');
- define('ROOT',str_replace($_SERVER['PHP_SELF'],'',str_replace('\\','/',__FILE__)));
- $conn=newcom('adodb.connection');
- $conn->Provider='microsoft.jet.oledb.4.0';
- $conn->Open(ROOT.'\\callerloc.mdb');//文件地址
- $rs=newcom('adodb.recordset');
- $rs->Open('SELECT * FROM '.TABLE,$conn, 1, 1);//查表
- $i= 0;
- $page=1;
- if(!emptyempty($_GET['page'])){
- $page=$_GET['page'];
- if($page<1)$page=1;
- if($page>$rs->PageCount)$page=$rs->PageCount;
- }
- $rs->AbsolutePage=$page;
- //打开文件
- $fp=fopen(OUTPUTFILE,"w");
- echo'<table width="100%"><tr>';
- foreach($rs->Fieldsas$name){
- echo'<td>'.$name->Name.'</td>';
- fwrite($fp,$name->Name."\t");
- }
- fwrite($fp,"\r\n");
- echo'</tr>';
- for(;!$rs->EOF;$rs->MoveNext()){
- echo'<tr>';
- foreach($rs->Fieldsas$name){
- //echo '<td>'.$name.'</td>';
- fwrite($fp,$name."\t");
- }
- $i++;
- fwrite($fp,"\r\n");
- if($i%1000==0)
- echo$i."<br>";
- echo'</tr>';
- if($rs->AbsolutePosition>=$rs->AbsolutePage*$rs->PageSize)
- break;
- }
- echo'</tale>';
- fclose($fp);
- ?>