PHP s Database Querying Methods

I l @ ve RuBoard

PHP's Database Querying Methods

PHP can connect to and query a database either using Open Database Connectivity (ODBC) or by directly accessing the database. The direct method uses a PHP extension that is written for the database in question (such as Oracle or MySQL). This extension can make use of the database's API to directly call the database.

However, you might find that your database has no PHP extension (although PHP does support most major database vendors ). If that is the case, you can use ODBC. (Most Database Management Systems [DBMSs] have an ODBC driver.)

ODBC

ODBC is a standard way of connecting a database to an application. ODBC is often seen on the Windows platform but is also available for UNIX and Linux systems.

ODBC lets you define a connection to a data source, as shown in Figure 6.1. This can be a flat-file database such as a CSV text file or a relational database. (Databases are often managed by a DBMS. A DBMS for a relational database is called a Relational Database Management System [RDBMS]).)

Figure 6.1. Connecting PHP to a database via ODBC.

graphics/06fig01.gif

Some DBMSs let you set quite advanced settings for connecting and querying a database. Such information is best collected and stored, and ODBC lets you do this. In fact, all you need to do is call the name of the ODBC connection, and all the details that are needed for connecting to a database are passed to the database to make that connection. An ODBC connection can be used by multiple applications. So, for example, if you need to query the same database using PHP or ASP, ODBC can make this much easier. ODBC calls a connection to a database a Data Source Name (DSN).

Appendix A, "Creating an ODBC Connection," gives details on setting up a DSN. ODBC lets you set different DSN types. The two types of DSNs of interest to us are System DSNs and User DSNs.

System DSNs

A System DSN is a connection to a database that can occur when the DSN and the database are on the same computer or on different computers, as shown in Figure 6.2.

Figure 6.2. Connecting PHP to a database via ODBC using a System DSN.

graphics/06fig02.gif

User DSNs

A User DSN is the reverse of a System DSN. A User DSN can connect only to a database that resides on the same computer that the DSN is set up on (see Figure 6.3).

Figure 6.3. Connecting PHP to a database via ODBC using a User DSN.

graphics/06fig03.gif

When setting up a DSN, it's important to choose the correct DSN for your system. In other words, if your database is located on a different computer than PHP, set up a System DSN. If it isn't, use either a System DSN or a User DSN. If you set up the wrong DSN for your system, the application that uses the DSN (such as PHP) reports an error ”normally the dreaded 80004005 error. If you get such an error and you can't find a problem with your application, double-check your DSN type.

For further information on ODBC, see http://www.microsoft.com/data.

The Direct Method

Most database vendors provide an API for connecting to and querying a database (see Figure 6.4). Using this API, applications can communicate directly with the database. PHP makes use of various database vendor APIs to achieve this aim. The MySQL API, for example, is compiled directly into PHP. Other database types such as SQL Server require their APIs be explicitly added via a PHP extension.

Figure 6.4. Connecting PHP to a database via the database API.

graphics/06fig04.gif

Using an API allows PHP to communicate directly with a database, so this method is often faster than ODBC, because you have no go-between. Your queries go directly to the database. PHP uses the direct method to query relational database systems such as SQL Server or Access, as well as flat-file databases such as text files or files in the dBASE file format.

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net