Pear DB Beginner's Guide Tutorial 1th/3 page _php Tutorial

Source: Internet
Author: User
Tags dsn getmessage pear sybase xml parser

1. Introduction This is a guide to how we use the Pear db extension. Pear DB, which provides a series of classes:
N Database Abstraction
N Advanced error handling mechanism
N and other

2. Download and install Pear
Since the Pear project is still in intensive development, the best way to get it is to get it from CVS (the Pear DB release has been bundled with the release of PHP4.0.6 later). So, we just need to put the Pear root directory in the php.ini configuration file include_path. You can also set this by: _set (' include_path ', '/pear_base_dir ').

The following is an example of STRP by step:

Directory to store Pear:
# Cd/usr/local/lib
Login with "PHPFI" Password:
# cvs-d:p server:cvsread@cvs.php.net:/repository Login
# cvs-d:p server:cvsread@cvs.php.net:/repository export-d "Last week" php4/pear
Edit the php.ini file plus the following paragraph at include_path: /usr/local/lib/php4/pearini_set (' include_path ', ' path_to_pear ');

Get full documentation of PHP CVS

Note that pear db must be PHP version 4.0.4 above, while some other packages in pear such as: XML Parser of the Pear Installer script need to PHP4.0.5 above version.

3. using Pear DB

3.1 Connect, 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();
?>
 

The data source (the $dsn parameter in the previous example) has the following format: (copied from the pear/db.php Parsedsn method)

 * 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

The supported databases are now available (in the phptype DSN section):

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, and a detailed list can be obtained from the root directory >/db/status .

3.2 Executing the database

 !--? php  
//Once you have a valid DB object
...
$sql = "SELECT * from clients" ;
//If The query is a "select", $db->query'll return
//A DB Result object on Success.
//Else It simply would return a DB_OK
//On Failure It'll return a DB Error Object.
$result = $db -> query ( $sql );
//Always check this $result is isn't an error
if ( DB :: I Serror ( $result ) {
die ( $result -> getMessage ());
}
....
?
 

3.3 Getting the data for select

http://www.bkjia.com/PHPjc/319757.html www.bkjia.com true http://www.bkjia.com/PHPjc/319757.html techarticle 1. Introduction This is a guide to how we use the Pear db extension. Pear DB, which provides a series of classes: N Database abstraction n Advanced error handling mechanism N and the other 2. Download, install ...

  • Related Article

    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.