Home > Articles > Programming > PHP

Multiple Database Interaction in PHP 4.1

  • Print
  • + Share This
Pack it up! Follow Cosentino's instructions and make your PHP code more portable and compatible with multiple databases.
This chapter is from the book

This chapter is from the book

Advanced Database Interaction in PHP4

If you have been playing with PHP for a while, you have most likely noticed its excellent support for connecting to MySQL databases. Most of the PHP books on the market describe PHP database support only with MySQL. But PHP supports many more databases with almost the same support it provides for MySQL. As of version 4.1, PHP supports the following databases in one form or another:

Basically, PHP has enough built-in support for a majority of your database needs, especially since it contains support for the commercial heavyweights, such as Oracle, Sybase, Informix, and Microsoft.

Unfortunately, each supported database has different functions to do the same things. For example, to connect to a MySQL database you use the function mysql_connect, and to connect to a MS SQL server you use mssql_connect. The two functions are almost identical, but have different names.

This causes problems as far as code portability goes. Say, for example, you have a killer app that you want to create, but management insists that you use Microsoft's SQL server as the database back-end. However, you know that MySQL or PostgreSQL will do the job just as well, and they are "free" databases that run on the lovely Linux server you have hiding under your desk. Wouldn't it be nice if you could code it once, then run your application using any one of these databases with only the flip of a variable to tell PHP which database it is talking to?

The PHP team has eliminated some of the problems with multiple databases by creating a database abstraction layer called DBX. DBX allows you to use one function that can, for example, connect to different types of databases.

You could also create your own set of custom database "wrappers" to allow you to support multiple databases.

There is also a great database abstraction layer called PEAR::DB. PEAR is a set of libraries for PHP that is similar to the PERL CPAN library, although not as extensive at this time.

This chapter will show you a little bit of all three solutions. The bottom line is that your code will be more portable.

  • + Share This
  • 🔖 Save To Your Account