05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

automatically assigned unique row identifier) are unavailable. Code that uses the PEAR<br />

DB is also typically a little slower than code that uses a database-specific extension.<br />

Keep in mind that an abstraction layer like PEAR DB does absolutely nothing when<br />

it comes to making sure your actual SQL queries are portable. If your application<br />

uses any sort of nongeneric SQL, you’ll have to do significant work to convert your<br />

queries from one database to another. For large applications, you should consider<br />

writing a functional abstraction layer; that is, for each database your application<br />

needs to support, write a set of functions that perform various database actions, such<br />

as get_user_record( ), insert_user_record( ), and whatever else you need, then have<br />

a configuration option that sets the type of database to which your application is<br />

connected. This approach lets you use all the intricacies of each database you choose<br />

to support without the performance penalty and limitations of an abstraction layer.<br />

For simple applications, we prefer the PEAR DB to the database-specific extensions,<br />

not just for portability but also for ease of use. The speed and feature costs are rarely<br />

significant enough to force us into using the database-specific extensions. For the most<br />

part, the rest of this chapter gives sample code using the PEAR DB abstraction objects.<br />

For most databases, you’ll need to recompile <strong>PHP</strong> with the appropriate database<br />

drivers built into it. This is necessary whether or not you use the PEAR DB library.<br />

The help information for the configure command in the <strong>PHP</strong> source distribution<br />

gives information on how to build <strong>PHP</strong> with support for various databases. For<br />

example:<br />

--with-mysql[=DIR] Include MySQL support. DIR is the MySQL base<br />

directory. If unspecified, the bundled MySQL<br />

library will be used.<br />

--with-oci8[=DIR] Include Oracle-oci8 support. Default DIR is<br />

ORACLE_HOME.<br />

--with-ibm-db2[=DIR] Include IBM DB2 support. DIR is the DB2 base<br />

install directory, defaults to<br />

/home/db2inst1/sqllib<br />

--with-pgsql[=DIR] Include PostgreSQL support. DIR is the PostgreSQL<br />

base install directory, defaults to<br />

/usr/local/pgsql.<br />

You can’t build <strong>PHP</strong> with support for a database whose client libraries you don’t<br />

have on your system. For example, if you don’t have the Oracle client libraries, you<br />

can’t build <strong>PHP</strong> with support for Oracle databases.<br />

Use the phpinfo( ) function to check for database support in your installation of<br />

<strong>PHP</strong>. For instance, if you see a section in the configuration report for MySQL, you<br />

know you have MySQL support.<br />

Relational Databases and SQL<br />

A Relational Database Management System (RDBMS) is a server that manages data<br />

for you. The data is structured into tables, where each table has some number of<br />

190 | Chapter 8: Databases<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!