12.2 The MySQL Module


Now that we have a basic idea of the concept behind our database-independent API, it's time to implement the wrapper functions for the different databases. The first database for which we are implementing a wrapper is MySQL. MySQL is a lightweight, fast database put out by T.c.X. Because of its speed and simplicity, MySQL is an ideal complement to PHP. That is why, as of PHP version 4, MySQL support is bundled with PHP. More information about MySQL can be found at http://www.mysql.com/.

 DB/mysql.php <?php include_once("DB/standard.php"); function db_connect($args=array()) {     switch (count($args)) {         case 0:             return @mysql_connect();         case 1:             return @mysql_connect($args[0]);         case 2:             return @mysql_connect($args[0], $args[1]);         default:             return @mysql_connect($args[0], $args[1], $args[2]);     } } function db_pconnect($args=array()) {     switch (count($args)) {         case 0:             return @mysql_pconnect();         case 1:             return @mysql_pconnect($args[0]);         case 2:             return @mysql_pconnect($args[0], $args[1]);         default:             return @mysql_pconnect($args[0], $args[1], $args[2]);     } } function db_select_db($args=array()) {     if (isset($args[1])) {         return@mysql_select_db($args[0], $args[1]);     }     return @mysql_select_db($args[0]); } function db_close($args=array()) {     return @mysql_close ($args[0]); } function db_query($args=array()) {     if (isset($args[1]) {         return @mysql_query($args[0], $args[1]);     }     return @mysql_query($args[0]); } function db_db_query($args=array()) {     if (isset($args[2])) {         return @mysql_db_query($args[0], $args[1],$args[2]);     }     return @mysql_db_query($args[0], $args[1]); } function db_prepare($args=array()) {     return db_simulate_prepare($args); } function db_execute($args=array()) {     $stmt = db_simulate_execute(&$args);     return db_query(array($stmt, array_shift ($args))); } function db_fetchrow($args=array()) {     if ($args[1] == DB_GETMODE_ASSOC) {         @mysql_fetch_array($args[0], MYSQL_ASSOC);     } elseif ($args[1] == DB_GETMODE_REG) {         return @mysql_fetch_array($args[0], MYSQL_NUM);     }     return @mysql_fetch_array($args[1]); } function db_num_rows($args=array()) {     return @mysql_num_rows($args[0]); } function db_commit($args=array()) {     return(true); } function db_rollback($args=array()) {     return(false); } function db_autoCommit($args=array()) {     return(true); } function db_free_result($args=array()) {     return @mysql_free_result($args[0]); } function db_errno($args=array()) {     return @mysql_errno(); } function db_error($args=array()) {     return @mysql_error(); } ?> 

Explanation

The MySQL API is very easy to create wrappers for because like the mSQL and the MSSQL modules, it is fairly standard. It has most functions that the other databases have, and it doesn't have many critical functions that the other databases don't have.

For arguments that can take a varying number of arguments (such as mysql_ connect() ), we use a switch..case loop to determine how many arguments to pass to the function. Something to notice is that for the maximum number of arguments, we don't use the argument number, but rather we use the default statement. This ensures that if we are moving from a database that requires a greater number of arguments to a database that requires fewer arguments, the database that requires fewer arguments still gets the arguments it needs.



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