PEAR Articles
  Home arrow PEAR Articles arrow Page 2 - Installing a PEAR Package
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PEAR ARTICLES

Installing a PEAR Package
By: David Web
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-09-24

    Table of Contents:
  • Installing a PEAR Package
  • Using the installed Packages
  • mdb2 Package
  • Mail Package
  • More Mail!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Installing a PEAR Package - Using the installed Packages


    (Page 2 of 5 )

    Let’s test the PEAR DB package. In a text editor add the following code:


    <?php

    // You know you're gonna need this

    // Imports all the PEAR::DB classes, static

    // methods, etc...

    require_once 'db.php';


    // Get the connection variables:

    include 'connx.php';

    // Build a DSN string (Data Source Name)

    // Required by DB::connect()

    $dsn = $dbtype . "://"

    .$dbuser . ":"

    . $dbpass . "@"

    . $dbhost . "/"

    . $dbname;



    // or, a database error object if it went wrong.

    // The boolean specifies this is a persistent

    // connection like mysql_pconnect(), it

    // defaults to FALSE.

    $db = DB::connect($dsn, TRUE);


    // Check whether the object is a connection or

    // an error.

    // Print out a message and exit if it's

    // an error object.

    if (DB::isError($db)) {

     

    die($db->getMessage());

     

    }else{


    print 'All is well';


    }

    // Close the connection.

    $db->disconnect();

    ?>

    The two includes at the beginning of the code block call the db class and a file called connx.php. The db file refers to the db package that we just installed, which can be accessed from anywhere in the system. And the connx.php file simply refers to a file that contains the database connection credentials, which are used to connect to the database.

    The next step is to create a Data Source Name or DSN, which is required by the DB::Connect() function:

    $dsn = $dbtype . "://"

    .$dbuser . ":"

    . $dbpass . "@"

    . $dbhost . "/"

    . $dbname;

    The parameters are all stored in the included connx.php file. Then the code actually connects to the database using the dsn:

    $db = DB::connect($dsn, TRUE);

    Then we check to see if the connection is an error and if so, we print out an appropriate message:

    if (DB::isError($db)) {

     

    die($db->getMessage());

     

    }else{


    print 'All is well';


    }

    Otherwise a positive message is printed out and the connection is closed:

    // Close the connection.

    $db->disconnect();

    When I run this code, I get the following result:


    More PEAR Articles Articles
    More By David Web


       · I have a question regarding PEAR,As a PHP Web Developer I am accustomed to...
     

    PEAR ARTICLES ARTICLES

    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning
    - Using XML_RPC2 with PEAR
    - Using Web Service APIs (Amazon and Yahoo!) w...
    - Database Abstraction with MDB2 from PEAR





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT