In PHP website development, because it supports various database engines, such as Mysql, Mssql, Pgsql, sqlite, etc., and provides various database systems Different functions are used as interfaces, which brings a lot of convenience to PHP website developers.
But it also brings about the problem of platform portability. As the underlying database changes, the PHP code must also change. There are various solutions to this problem, such as using the PHP ADODB class, PHP PEAR DB class or writing your own PHP DB class to aggregate the function operations of various databases, etc. Today I will share with you how to install and use the PHP PEAR DB class , to achieve access functions of different databases.
Preparation work
1. Before using the PHP PEAR DB class to access the database, you need to install PHP PEAR, and then download and install the DB class through PEAR, that is, pear install db
2. Install relevant databases as needed, such as Mysql, Mssql, Pgsql, Sqlite, etc. At the same time, find Dynamic Extensions in PHP.INI, introduce the DLL file corresponding to the data, and restart Apache.
Note: Since I am using DedeAMPZ, when installing PHP PEAR, it must be installed in the DedeAMPZWebRootDefault directory. Otherwise, when introducing DB.php, a Failedopening required 'DB.php' error will be reported, that is, it cannot be found. DB class (could not find pear db library), because DedeAMPZ may have restrictions on related directories.
Example of using PHP PEAR DB class
<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></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><span>$result-</span><span class="tag">></span><span>numRows();$i++) </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><span class="tag-name">br</span><span class="tag">></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>
The above is a detailed introduction to the use of PHP PEAR DB class.