Heim >php教程 >php手册 >PHP通过ADO方式连接Access

PHP通过ADO方式连接Access

WBOY
WBOYOriginal
2016-06-21 08:52:362646Durchsuche

PHP通过ADO方式连接Access数据库,如下代码:

<ol class="dp-xml">
<li class="alt"><span><span>nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  </span></span></li>
<li><span>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"<span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">html</span><span> </span><span class="attribute">xmlns</span><span>=</span><span class="attribute-value">"http://www.w3.org/1999/xhtml"</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">title</span><span class="tag">></span><span>通过ADO方式连接ACCESS数据库 - www.cxybl.com</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">link</span><span> </span><span class="attribute">rel</span><span>=</span><span class="attribute-value">"stylesheet"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"style.css"</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="comments"><!--</span> </span></li><li><span><span class="comments">.STYLE1 {color: #FFFFFF}</span> </span></li><li class="alt"><span><span class="comments">--></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">style</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li class="alt"><span>include_once("conn.php"); </span></li>
<li><span><span class="tag">?></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">body</span><span> </span><span class="attribute">topmargin</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">leftmargin</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">bottommargin</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"300"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"25"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#0099CC"</span><span class="tag">></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"STYLE1"</span><span class="tag">></span><span>图书信息</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>  <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#0099CC"</span><span class="tag">></span><span class="tag"><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"300"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"50"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span>  </span></span></span></span></li>
<li><span>    <span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"1"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>      <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>        <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"167"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"25"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#FFFFFF"</span><span class="tag">></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span>书名</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"130"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#FFFFFF"</span><span class="tag">></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span>出版社</span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>      <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>      <span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li><span>       $<span class="attribute">sql</span><span>=</span><span class="attribute-value">"select * from tb_book"</span><span>; </span></span></li>
<li class="alt"><span>       $<span class="attribute">rs</span><span>=</span><span class="attribute-value">new</span><span> com("adodb.recordset"); </span></span></li>
<li><span>       $rs-<span class="tag">></span><span>open($sql,$conn,1,3); </span></span></li>
<li class="alt"><span>       while(!$rs-<span class="tag">></span><span>eof) </span></span></li>
<li><span>       { </span></li>
<li class="alt"><span>      <span class="tag">?></span><span> </span></span></li>
<li><span>      <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"25"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#FFFFFF"</span><span class="tag">></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>      <span class="tag"></span><span class="tag-name">php</span><span> $</span><span class="attribute">fields</span><span>=$rs-</span><span class="tag">></span><span>fields(bookname);echo $fields-</span><span class="tag">></span><span>value;</span><span class="tag">?></span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>        <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"25"</span><span> </span><span class="attribute">bgcolor</span><span>=</span><span class="attribute-value">"#FFFFFF"</span><span class="tag">></span><span class="tag"><span class="tag-name">div</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span></span></span></span></li>
<li><span>      <span class="tag"></span><span class="tag-name">php</span><span> $</span><span class="attribute">fields</span><span>=$rs-</span><span class="tag">></span><span>fields(pub);echo $fields-</span><span class="tag">></span><span>value;</span><span class="tag">?></span><span class="tag"></span><span class="tag-name">div</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>      <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>      <span class="tag"></span><span class="tag-name">php</span><span> </span></span></li>
<li class="alt"><span>         $rs-<span class="tag">></span><span>movenext; </span></span></li>
<li><span>       } </span></li>
<li class="alt"><span>      <span class="tag">?></span><span> </span></span></li>
<li><span>    <span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"></span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
</ol>

conn.php:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span><span class="vars">$conn</span><span> = </span><span class="keyword">new</span><span> com(</span><span class="string">"adodb.connection"</span><span>);   </span></span></li>
<li class="alt"><span><span class="vars">$connstr</span><span>=</span><span class="string">"driver={microsoft access driver (*.mdb)}; dbq="</span><span>. </span><span class="func">realpath</span><span>(</span><span class="string">"data/db_database12_185.mdb"</span><span>); </span></span></li>
<li><span><span class="vars">$conn</span><span>->open(</span><span class="vars">$connstr</span><span>); </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn