Home arrow PHP arrow Database Techniques and PHP

Database Techniques and PHP

Picking up where we left off last week in our discussion of databases and PHP, we'll talk about connecting, issuing a query, and more. This article is excerpted from chapter eight of the book Programming PHP, Second Edition, written by Kevin Tatroe, Rasmus Lerdorf, and Peter MacIntyre (O'Reilly, 2006; ISBN: 0596006810). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

TABLE OF CONTENTS:
  1. Database Techniques and PHP
  2. Issuing a Query
  3. Inside a row array
  4. Advanced Database Techniques
By: O'Reilly Media
Rating: starstarstarstarstar / 4
June 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 Connecting 

Once you have a DSN, create a connection to the database using the connect() method. This returns a database object you’ll use for tasks such as issuing queries and quoting parameters:

  $db = DB::connect(DSN [, options ]);

Theoption value can either be Boolean, indicating whether or not the connection is to be persistent, or an array of options settings. Theoption values are given in Table 8-2.

Table 8-2. Connection options

Option Controls

persistent

Connection persists between accesses

optimize

What to optimize for
debug Display debugging information

By default, the connection is not persistent and no debugging information is displayed. Permitted values foroptimizeare'performance'and'portability'. The default is'performance'. Here’s how to enable debugging and optimize for portability:

  $db = DB::connect($dsn, array('debug' => 1, 'optimize' => 'portability'));

Error Checking

PEAR DB methods return DB_ERROR if an error occurs. You can check for this with DB::isError():

  $db = DB::connect($datasource);
  if (DB::isError($db)) {
   
die($db->getMessage());
  }

TheDB::isError()method returnstrueif an error occurred while working with the database object. If there was an error, the usual behavior is to stop the program and display the error message reported by thegetMessage()method. You can callgetMessage()on any PEAR DB object.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: