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

Source: Internet
Author: User
Tags dsn error handling getmessage microsoft sql server odbc 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 package has been bundled with the PHP4.0.6 version). So we just need to put Pear's root directory in the php.ini profile include_path. You can also set this by: _set (' include_path ', '/pear_base_dir ').

The following is an example of STRP by:

  store  Pe Ar 
  # cd/usr/local/lib  
  Phpfi password login : 
  # cvs-d:p server:cvsread@cvs.php.net:/repository login  
  pear "Today", "Last month",  "Last week"  Bugs 
  # cvs-d:p server:cvsread@cvs.php.net:/repository export-d "Last week" Php4/pear  
 Font face= "Bold" edit the  php.ini file plus the following paragraph in  include_path /usr/ Local/lib/php4/pear    ini_set (' include_path ', ' path_to_ Pear ');  

Get full documentation of PHP CVS

Note that the Pear db required 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 the above version.

3. using Pear DB

3.1 Connections, disconnecting 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 with:
$db->disconnect();
?>
 

Data Source ( $dsn parameter in the example above ) There are the following allowed formats: (from pear/db.php parsedsn method copy)

 
     *  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 now have ( 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 you can get a detailed listing from the root directory >/db/status .

3.2 Execution Database

 
<?php
// 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 Get the data for the Select

Current 1/3 page 123 Next read the full text

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.