Home arrow MySQL arrow Page 3 - PHP, MySQL and the PEAR Database

Creating a connect instance - MySQL

In this conclusion to a three-part series, you will add PEAR into the mix of what you have already learned. This article is excerpted from chapter 9 of Learning PHP and MySQL, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Copyright 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

  1. PHP, MySQL and the PEAR Database
  2. Rewriting the Books Example with PEAR
  3. Creating a connect instance
  4. PEAR error reporting
By: O'Reilly Media
Rating: starstarstarstarstar / 8
May 31, 2007

print this article



The DB.php file defines a class of type DB. Refer to Chapter 5 for more information on working with classes and objects. We'll principally be calling the methods in the class. The DB class has a connect method, which we'll use instead of our old connect function mysql_connect. The double colons (::) indicate that we're calling that function from the class in line 4:

  connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

When you call the connect function, it creates a new database connection that is stored in the variable $connection. The connect function attempts to connect to the database based on the connect string you passed to it.

Connect string

The connect string uses this new format to represent the login information that you already supplied in separate fields:


This format may look familiar to you, as it's very similar to the connect string for a Windows share. The first part of the string is what really sets the PEAR functions apart from the plain PHP. The phptype field specifies the type of database to connect. Supported databases include ibase, msql, mssql, mysql, oci8, odbc, pgsql, and sybase. All that's required for your PHP page to work with a different type of database is changing the phptype!

The username ,  passwordhost, and database should be familiar from the basic PHP connect. Only the type of connection is required. However, you'll usually want to specify all fields.

After the values from dblogin.php are included, the connect string looks like the following:


If the connect method on line 6 was successful, a $DB object is created. It contains the methods to access the database as well as all of the information about the state of that database connection.


One of the methods it contains is called query. The query method works just like PHP's query function in that it takes a SQL statement. The difference is the hyphen and greater-than syntax (->) is used to call it from the object. It also returns the results as another object instead of a result set.

  $query = "SELECT * FROM `books`"
  $result = $connection->query($query);

Based on the SQL query, this code calls the query function from the connection object and returns a result object named $result.


Line 22 uses the result object to call the fetchRow method. It returns the rows one at a time, similar to mysql_fetch_row.

  while ($result_row = $result->fetchRow()) {
      echo 'Title: '.$result_row[1] . '<br />';
      echo 'Author: '.$result_row[4] . '<br />';
      echo 'Pages: '.$result_row[2] . '<br /><br />';

You use another while loop to go through each row from fetchRow until it returns FALSE. The code in the loop hasn't changed from the non-PEAR example.


In line 30, you're finished with the database connection, so you close it using the object method disconnect:


>>> More MySQL Articles          >>> More By O'Reilly Media

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: