12.1 The Glue


If we were using the object-oriented approach to database independence with PHP, we would need a whole lot of code to tie everything together. However, with the function-oriented interface, the code becomes simpler. Just include the right header file and you're set. For example, to use a MySQL database the include line would be include_once("DB/mysql.php") , and to include and work with an Oracle database, the line would be include_once("DB/oracle.php") .

Also note that all function calls will accept only one parameter ”an array that contains the real parameters. This is used so that your function take an optional number of arguments, in case the function call for one database requires more arguments than a function call for another database. (It is also done this way for compatibility with PHP 3; in PHP 4, you can use a combination of func_num_args() , func_get_arg (), and func_get_args() to achieve the same effect.)

One thing that's always nice, and not too hard to implement, is a standard set of often-needed functions. These can be API functions (such as db_fetchall() in the following code), or they can be internal functions (such as db_simulate_prepare() , also in the following code). We're going to place these functions into a file called DB/standard.php, and then include them in all the different modules so that they will be available to the user .

 DB/standard.php <?php function &db_fetchall($args=array()) {     $rows = array();     while ($row = db_fetchrow($args[0], DB_GETMODE_NUM)) {         array_push($rows, &$row);     }     return($rows); } function &db_fetchall_assoc($args=array()) {     $rows = array();     while ($row = db_fetchrow($args[0], DB_GETMODE_ASSOC)) {         array_push($rows, &$row);     }     return ($rows); } function db_simulate_prepare($args=array()) {     return array_shift($args); } function db_simulate_execute($args=array()) {     $stmt     = array_shift($args);     $prepArgs = array_shift($args);     if (!is_array($prepArgs)) {         return($stmt);     }     $parts = explode('?', $stmt);     foreach ($parts as $part) {         $new_stmt .= $part . array_shift($args);     }     return($new_stmt); } ?> 

The db _ fetchall () and db _ fetchall_assoc() functions use the database-independent API that we've created to implement commonly needed and useful functions for manipulating result sets using PHP. Both functions build an array of all the rows in the result set (given by the first argument). You can use these functions like so:

 <?php include_once("DB/mysql.php"); $dbh = db_connect(array("localhost", "username", "password")); if (!$dbh) {     die(sprintf("An error occurred, %s: %s", db_errno(), db_error())); } $sth = db_query("SELECT * FROM some table"); if (!$sth) {      die(sprintf("Couldn't execute query, %s: %s", db_errno(), db_error())); } $rows = db_fetchall_assoc($sth); foreach ($rows as $row) {     print $row[firstname];     print $row[lastname];     print $row[occupation]; } db_free_result($sth); db_close($dbh); ?> 

The db_simulate_prepare() and db_simulate_execute() functions will simulate the prepare and execute functions for databases where those features are not supported. The method we use is quite easy. The db_simulate_prepare() function is simply a dummy ; it takes the query that needs to be prepared and returns it to the user.

The db_simulate_execute() function is a little more advanced. First, it splits the string by the '?' delimiter (using the explode() function), and then it concatenates the appropriate string from the user's array. The db_simulate_execute() function then returns the query that should be executed.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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