Database Articles

  Home arrow Database Articles arrow Page 3 - Database Abstraction with PEAR
DATABASE ARTICLES

Database Abstraction with PEAR
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-07-28

    Table of Contents:
  • Database Abstraction with PEAR
  • What is PEAR?
  • DB::connect
  • DB::disconnect
  • DB::isError
  • DB::getOne and DB::query()
  • DB_Result::fetchRow

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Database Abstraction with PEAR - DB::connect


    (Page 3 of 7 )

    mixed connect (string $dsn [, array $options])

    That is our connect function. It is what we will use to connect to the database. Its actual use will look something like:

    $db = DB::connect("mysql://user:pass@host/db_name", FALSE);

    It will return a connection object or an error object which will be stored in $db. You may be wondering what that wierd :: is. Well, DB is the class for data abstraction and connect is the function. Normally you would see it look like DB->connect(). The reason for the :: is so that we can use the connect function without creating an instance of the class DB. Maybe a little bit different than you are used to, but it has its uses. The :: can also be used to access a function of a base class when you are dealing with inherited classes.

    You can see that we passed the connect function what sort of looks like a URL. This is a dsn, or data source name. Here you pass it the database type and other connection information such as username and password. Here is a list of supported database types:mysql -> MySQLpgsql -> PostgreSQLibase -> InterBasemsql -> Mini SQLmssql -> Microsoft SQL Serveroci8 -> Oracle 7/8/8iodbc -> ODBC (Open Database Connectivity)sybase -> SyBaseifx -> Informixfbsql -> FrontBase

    The second, optional parameter is whether to use persistent connections or not. Not all databases support persistent connections and the default for this parameter is false.

    Here is how it is commonly used:

    <?php
    require_once 'DB.php';

    $user 'foo';
    $pass 'bar';
    $host 'localhost';
    $db_name 'clients_db';

    $dsn "mysql://$user:$pass@$host/$db_name";

    $db DB::connect($dsnfalse);
    ?>

    More Database Articles Articles
    More By Matt Wade

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap