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:

  • MySQL www.mysql.com

  • mSQL www.hughes.com.au

  • MS SQL (Microsoft SQL server; on Win32 systems only)

  • filePro (Read only) www.fptech.com

  • Informix (from IBM) www.informix.com

  • InterBase www.interbase.com

  • Oracle www.oracle.com

  • Ovrimos www.ovrimos.com

  • Sybase www.sybase.com

  • DB++ experimental www.concept-asa.de

  • DBM flat file databases available on many flavors of *nix

  • PostgreSQL www.pgsql.com

  • Frontbase www.frontbase.com

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.


   
Top


Advanced PHP for Web Professionals
Advanced PHP for Web Professionals
ISBN: 0130085391
EAN: 2147483647
Year: 2005
Pages: 92

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