Introduction
What is good about the Database Abstraction Layer?
The most important benefit of the DBAL is allowing phpBB to support several database software packages without bloating of the forum code. No PHP features for multiple database support existed when phpBB 2.0 was created, so a new system was developed through the DBAL. This portability was a major factor in the rise of phpBB as a popular discussion forum solution.
Since the PHP functions for different databases have different names, using them without a DBAL would mean phpBB would consist of much larger files or support only one or two database types. The DBAL allows phpBB to be coded for potentially any type of database supported by PHP with the addition of merely one file per database type. If the DBAL does not include a coder's favorite type of database software, they can extend the DBAL by creating a new file to add that support.
Modifications, hacks, and add-ons (referenced simply as "hacks" henceforth) can also use the DBAL just like the original phpBB code. A hack written for the DBAL with standard SQL queries can be used on several types of databases, while a hack written with the PHP functions can only be used on the type supported by those functions. The DBAL allows hack authors to create cross-platform scripts that can be used by a wider audience.
What is bad about the Database Abstraction Layer?
Nothing is perfect, and the Database Abstraction Layer is no exception.
The absolute largest problem is that the DBAL is not as familiar to coders as the PHP functions. phpBB, while a very popular forum software, has not been around as long as PHP and is not likely to have been used by nearly as many coders. Using phpBB's DBAL means learning how to use a new set of functions for which there has been no documentation available. There has been no method of learning how to use the DBAL other than examining the files and experimentation. One of the goals of this document is to correct this problem.
The second problem is that the DBAL is not compatible with all types of software. For example, a certain popular server caching software can cause the DBAL to slow down a site and even prevent it from working. Also, some server settings can prevent more than one database connection from being opened in a script (the DBAL and PHP database functions cannot be used in the same script in these cases).
Should hack authors write code using the Database Abstraction Layer? Why?
Yes, hack authors should definitely write code using the Database Abstraction Layer whenever possible.
The most important reason behind this statement is not that phpBB uses the DBAL, but rather that phpBB forum administrators rely on the DBAL. If a hack author writes code using the PHP functions for MySQL, that can only be used on sites using MySQL. Any user of the Oracle, PostgreSQL, Microsoft Access, or Microsoft SQL database software will be unable to use the hack. If the code had been written to use the DBAL, these users might have been able to use it as well.
While the majority of phpBB forums do run on MySQL databases, users of other database software packages are not a minority that should be ignored. A good web designer knows that you do not design a site to work only in one browser, because site users may use any of a hundred different browsers. The same holds true for coding phpBB hacks that access the database - hack users may use any of several database software packages.
Another reason to use the DBAL is efficiency. Any file that includes phpBB's db.php (or common.php, which includes that file) will establish a database connection that will remain open until the script ends or the connection is closed within the script. Using PHP database functions in these files may establish a second database connection, resulting in the use of excess server resources and processing time. There may also be server settings that prevent the second connection from being established, which would cause the file to function improperly.
How to use the Database Abstraction Layer
Once you get the hang of it, the Database Abstraction Layer is deceptively simple to use.
The DBAL is a series of variables and functions operating on those variables, all of which are related to the database connection. In PHP, this is called a class. All of the DBAL's variables and functions can be used outside of the class, making them available in any part of phpBB's code after the database connection is established.
You can use the variables and functions as you would use normal variables and functions, with one exception. To use a normal variable, you would write it as . Likewise, a normal function call would be written as function_name(). For DBAL functions and variables, these references would be written as and ().