Getting started with PearDB

Source: Internet
Author: User
You can use PearDB to obtain more useful data information from the query results. These methods include numRows (): The number of data returned by a SELECT query.

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 in the code: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

 

      
// 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 with:
$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@protocol+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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.