Heim > Backend-Entwicklung > PHP-Tutorial > PHP PEAR DB类在网站建设中的作用体现_PHP教程

PHP PEAR DB类在网站建设中的作用体现_PHP教程

WBOY
Freigeben: 2016-07-15 13:33:53
Original
984 Leute haben es durchsucht

在PHP网站开发中,由于其支持各种数据库引擎,如Mysql,Mssql,Pgsql,sqlite等,并对各种数据库系统都提供了不同的函数作为接口,给PHP网站开发者带来很多便利。

但同时也带来了平台移植性的问题,随着底层数据库的改变,PHP代码也必须改变。对于这个问题,有各种解决方法,如使用PHP ADODB类,PHP PEAR DB类或者自行编写PHP DB类,将各种数据库的函数操作聚合在一起等,今天和大家分享如何安装使用PHP PEAR DB类,以实现不同数据库的访问功能。

准备工作

1、在使用PHP PEAR DB类访问数据库之前,需要安装PHP PEAR,然后通过PEAR来下载安装DB类,即pear install db

2、根据需要安装相关数据库,比如Mysql,Mssql,Pgsql,Sqlite等,同时在PHP.INI中找到Dynamic Extensions,引入对应数据的DLL文件,并重启Apache。

注意:由于我使用的是DedeAMPZ,在安装PHP PEAR时一定要安装在DedeAMPZWebRootDefault目录下,否则在引入DB.php时会报Failedopening required ‘DB.php’错误,即找不到DB类(could not find pear db library),因为DedeAMPZ可能对相关的目录做了限制。

PHP PEAR DB类使用示例

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?  </span></span></span></li>
<li><span>require_once("DB.php");  </span></li>
<li class="alt">
<span>$</span><span class="attribute">userName</span><span> = </span><span class="attribute-value">'root'</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">password</span><span> = </span><span class="attribute-value">'123456'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">hostName</span><span> = </span><span class="attribute-value">'localhost'</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">dbName</span><span> = </span><span class="attribute-value">'test'</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">dsn</span><span> = </span><span class="attribute-value">"mysql://$userName:<br>$password@$hostName/$dbName"</span><span>;  </span>
</li>
<li>
<span>$</span><span class="attribute">dbCon</span><span> = </span><span class="attribute-value">DB</span><span>::connect($dsn);  </span>
</li>
<li class="alt"><span>if (DB::isError($dbCon)) {  </span></li>
<li>
<span>die ($dbCon-</span><span class="tag">></span><span>getMessage());  </span>
</li>
<li class="alt"><span>}  </span></li>
<li>
<span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"CREATE TABLE leapsoul ("</span><span>.  </span>
</li>
<li class="alt"><span>"`id` INT( 11 ) UNSIGNED NOT NULL ,".  </span></li>
<li><span>"`name` VARCHAR( 30 ) CHARACTER <br>SET gbk COLLATE gbk_chinese_ci NOT NULL ,".  </span></li>
<li class="alt"><span>"`age` INT( 2 ) NOT NULL ,".  </span></li>
<li><span>"`birthday` VARCHAR( 30 ) CHARACTER <br>SET gbk COLLATE gbk_chinese_ci NOT NULL ,".  </span></li>
<li class="alt"><span>"`sex` INT( 1 ) NOT NULL ,".  </span></li>
<li><span>"PRIMARY KEY ( `id` )".  </span></li>
<li class="alt">
<span>") </span><span class="attribute">ENGINE</span><span> = </span><span class="attribute-value">MYISAM</span><span> CHARACTER SET gbk <br>COLLATE gbk_chinese_ci";  </span>
</li>
<li>
<span>$</span><span class="attribute">result</span><span> = $dbCon-</span><span class="tag">></span><span>query($sql);  </span>
</li>
<li class="alt"><span>if (DB::isError($result)) {  </span></li>
<li>
<span>die ($result-</span><span class="tag">></span><span>getMessage());  </span>
</li>
<li class="alt"><span>}  </span></li>
<li>
<span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"insert into leapsoul(id,name,<br>age,birthday,sex) values(1,'leapsoul',1,<br>'2009-05-13',1),(2,'leapsoul',1,'2009-05<br>-13',1),(3,'leapsoul',1,'2009-05-13',1)"</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span> = $dbCon-</span><span class="tag">></span><span>query($sql);  </span>
</li>
<li><span>if (DB::isError($result)) {  </span></li>
<li class="alt">
<span>die ($result-</span><span class="tag">></span><span>getMessage());  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>$dbCon-</span><span class="tag">></span><span>setFetchMode(DB_FETCHMODE_ASSOC);  </span>
</li>
<li>
<span>$</span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select * from leapsoul"</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span> = $dbCon-</span><span class="tag">></span><span>query($sql);  </span>
</li>
<li><span>if (DB::isError($result)) {  </span></li>
<li class="alt">
<span>die ($result-</span><span class="tag">></span><span>getMessage());  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>;$i</span><span class="tag"><span>$result-</span><span class="tag">></span><span>numRows();$i++)  </span></span>
</li>
<li><span>{  </span></li>
<li class="alt">
<span>$</span><span class="attribute">info</span><span> = &$result-</span><span class="tag">></span><span>fetchRow();  </span>
</li>
<li><span>echo "name:".$info['name'];  </span></li>
<li class="alt">
<span>echo "birthday:".$info['birthday']."</span><span class="tag"><span class="tag-name">br</span><span class="tag">></span><span>";  </span></span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>$result-</span><span class="tag">></span><span>free();  </span>
</li>
<li>
<span>$dbCon-</span><span class="tag">></span><span>disconnect();  </span>
</li>
<li class="alt"><span>}  </span></li>
</ol>
Nach dem Login kopieren

以上就是PHP PEAR DB类的详细使用方法介绍。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446030.htmlTechArticle在PHP网站开发中,由于其支持各种数据库引擎,如Mysql,Mssql,Pgsql,sqlite等,并对各种数据库系统都提供了不同的函数作为接口,给PHP网站...
Quelle:php.cn
Erklärung dieser Website
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
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage