In the development of PHP Web site, because it supports various database engines, such as Mysql,mssql,pgsql,sqlite, and provides different functions as interface for various database systems, it brings many conveniences to PHP website developers.
But it also brings the problem of platform portability, and the PHP code must change as the underlying database changes. For this problem, there are various solutions, such as the use of PHP ADODB class, PHP PEAR DB class, or write PHP DB class, the various functions of the database aggregation together, today and we share how to install the use of PHP PEAR DB class, to achieve different database access capabilities.
Preparatory work
1, before using the PHP PEAR DB class to access the database, you need to install PHP PEAR, and then through the PEAR to download the installation DB class, that is, PEAR install DB
2, according to the need to install relevant databases, such as Mysql,mssql,pgsql,sqlite, while in php.ini find dynamic Extensions, the introduction of the corresponding data DLL file, and restart Apache.
Note: Since I am using Dedeampz, install PHP pear must be installed in the Dedeampzwebrootdefault directory, or in the introduction of db.php will be reported failedopening required ' db.php ' ERROR, The DB class could not be found (could not find Pear DB Library) because Dedeampz might restrict the associated directory.
PHP PEAR DB Class Use example
- < ?
- Require_once ("db.php");
- $ UserName = ' Root ' ;
- $ Password = ' 123456 ' ;
- $ HostName = ' localhost ' ;
- $ DbName = ' Test ' ;
- $ DSN = "mysql://$userName:
$password @ $hostName/$dbName ";
- $ Dbcon = DB :: Connect ($DSN);
- if (Db::iserror ($dbCon)) {
- Die ($dbCon->getMessage ());
- }
- $ SQL = "CREATE TABLE Leapsoul (".
- "' ID ' INT (one) UNSIGNED not NULL,".
- "' Name ' VARCHAR (CHARACTER)
SET GBK COLLATE gbk_chinese_ci not NULL, ".
- "' Age ' INT (2) is not NULL,".
- "' Birthday ' VARCHAR (CHARACTER)
SET GBK COLLATE gbk_chinese_ci not NULL, ".
- "' Sex ' INT (1) Not NULL,".
- "PRIMARY KEY (' id ')".
- ") ENGINE = MYISAM CHARACTER SET GBK
COLLATE Gbk_chinese_ci ";
- $ result = $dbCon- > query ($sql);
- if (Db::iserror ($result)) {
- Die ($result->getMessage ());
- }
- $ SQL = INSERT INTO Leapsoul (Id,name,
Age,birthday,sex) VALUES (1, ' Leapsoul ', 1,
' 2009-05-13 ', 1), (2, ' Leapsoul ', 1, ' 2009-05
-13 ', 1), (3, ' Leapsoul ', 1, ' 2009-05-13 ', 1) ";
- $ result = $dbCon- > query ($sql);
- if (Db::iserror ($result)) {
- Die ($result->getMessage ());
- }
- $dbCon- > Setfetchmode (DB_FETCHMODE_ASSOC);
- $ SQL = "SELECT * from Leapsoul" ;
- $ result = $dbCon- > query ($sql);
- if (Db::iserror ($result)) {
- Die ($result->getMessage ());
- }
- For ($i=0; $i<$result-> NumRows (); $i + +)
- {
- $ Info = & $result- > Fetchrow ();
- echo "Name:". $info [' name '];
- echo "Birthday:". $info [' Birthday ']. " < BR > ";
- }
- $result- > Free ();
- $dbCon- > disconnect ();
- }
These are the details of how PHP PEAR DB classes are used.
http://www.bkjia.com/PHPjc/446030.html www.bkjia.com true http://www.bkjia.com/PHPjc/446030.html techarticle in the development of PHP Web site, because it supports a variety of database engine, such as Mysql,mssql,pgsql,sqlite, and various database systems are provided with different functions as interface, to the PHP site ...