1. Introduction This is a guide on how to use pear dB extensions. Pear dB provides the following classes:
N database abstraction
N advanced Error Handling Mechanism
N and others
2. Download and install Pear
Because the pear project is still under intensive development, the best way to get it is to obtain it from CVS (the pear dB release package has been bundled with php4.0.6 and later versions ). Therefore, we only need to put the pear root directory in the plude_path configuration file of PHP. ini. You can also set it as follows:_ Set ('include _ path', '/pear_base_dir ').
The following is an example of STRP by step:
Directory for storing pear: # Cd/usr/local/lib Use the "phpfi" password to log on: # CVS-D: pserver: cvsread@cvs.php.net:/Repository Login Run the following command to obtain all the pear files and update the downloaded files. Other parameters include "today", "last month", and so on. We recommend that you use the "Last week" parameter, because generally, the bugs is submitted and modified once a week. # CVS-D: pserver: cvsread@cvs.php.net:/Repository export-d "last week" PhP4/Pear Edit the php. ini file and add the following section at include_path:/Usr/local/lib/PhP4/PearIf you do not have the modification permission, you can use this statement inCodeImplementation in:Ini_set ('include _ path', 'path _ to_pear '); |
Obtain the complete php cvs documentation
Note that pear dB must be PHP version 4.0.4 or later, and some other packages in pear, such as XML Parser of the pear installer script, must be php4.0.5 or later.
3.UsePear DB
3.1 connection, disconnect the database
<? PHP
// The pear base directory must be in your include_path
Require_once 'Db. php' ;
$ User = 'Foo' ;
$ Pass = 'Bar' ;
$ Host = 'Localhost' ;
$ Db_name = 'Clients _ db' ;
// Data source name: This is the universal connection string
$ DSN
= "MYSQL: // $ User: $ pass @ $ host/$ db_name" ;
// DB: connect will return a pear dB object on success
// Or a pear dB error object on Error
// You can also set to true the second Param
// If you want a persistent connection:
// $ Db = DB: connect ($ DSN, true );
$ DB = DB :: Connect ( $ DSN );
// With DB: iserror you can differentiate between an error or
// A valid connection.
If ( DB :: Iserror ( $ DB )){
Die ( $ DB -> Getmessage ());
}
....
// You can disconnect from the database:
$ DB -> Disconnect ();
?>
|
Data Source (in the previous example$ DSNThe format is as follows: (copied from the parsedsn method of pear/DB. php)
* Phptype: database backend used in PHP (MySQL, ODBC etc .)
* Dbsyntax: database used with regards to SQL syntax etc.
* Protocol: communication protocol to use (TCP, Unix etc .)
* Hostspec: Host specification (hostname [: Port])
* Database: database to use on the DBMS Server
* Username: User Name For Login
* Password: Password for login
*
* The format of the supplied DSN is in its fullest form:
*
* Phptype (dbsyntax): // username: password @ protocol + hostspec/Database
*
* Most variations are allowed:
*
* Phptype: // username: password @ protocols + hostspec: 110 // usr/db_file.db
* Phptype: // username: password @ hostspec/database_name
* Phptype: // username: password @ hostspec
* Phptype: // username @ hostspec
* Phptype: // hostspec/Database
* Phptype: // hostspec
* Phptype (dbsyntax)
* Phptype
|
Currently, the supported databases includePhptypeDSN part ):
Mysql> MySQL
Pgsql-> PostgreSQL
IBase-> InterBase
Msql-> Mini SQL
MSSQL> Microsoft SQL Server
Oci8-> oracle 7/8/8i
ODBC-> ODBC (Open Database Connectivity)
Sybase-> Sybase
Ifx-> Informix
Fbsql-> frontbase
|
Note that not all database features are supported. Root directory>/DB/statusGet a detailed list.
3.2 execution database
// once you have a valid dB object ... $ SQL = "select * from clients" ; // If the query is a "select ", $ db-> query will return // a DB result object on success. // else it simply will return a db_ OK // on failure it will return a DB error object. $ result = $ db -> query ( $ SQL ); // always check that $ result is not an error If ( DB :: iserror ( $ result )) { die ( $ result -> getmessage (); } .... ?> |
3.3 obtain select data