Warning: include(header.php) [function.include]: failed to open stream: No such file or directory in /home1/taskboyc/public_html/programs/php_sql/index.php on line 4

Warning: include() [function.include]: Failed opening 'header.php' for inclusion (include_path='/home/jjohn/sites/taskboy/www') in /home1/taskboyc/public_html/programs/php_sql/index.php on line 4

DB: A PHP class for MySQL

I present this SQL interface to MySQL as an example of the kind of abstraction I've come to use when dealing with SQL. It's true that PHP does quite have the DBI of Perl, but it does have a DB abstraction layer that's worth using.

However, this abstraction wasn't available when I started with PHP, so I wrote my own. I've used this in a variety of projects and although it's not complete, it's pretty useful for my needs. Not that the code also looks for the existence of a global $CONF hash, which is a common idiom for many PHP apps I've run across. The initialization parameters can come from this global hash instead of being passed to the object at instantiation.

The following PHP 4/5 code is an example of how to use this class.

<?php
$db = new db(array("DB_HOST" => "localhost",
                 "DB_NAME" => "my_database",
                 "DB_USER" => "guest",
                 "DB_PASS" => "s3cr3t",
                 "LOGFILE" => "/tmp/sql.log",
                ));

$fields = array("*",
                "DATE_FORMAT(created,'%Y-%M-%B') as p_date'
               );

$db->sql_select(array("table" => "users",
                      "fields" => $fields,
                      "where" => "credits > 5",
                      "order_by" => "created",
                      ));

$id = $db->sql_insert(array("table" => "users",
                      "values" => array("name" => "todd",
                                        "credits"=>10),
                      "insert_id" => 1,
              ));

$db->sql_update(array("table" => "users",
                      "values" => array("credits" => 20),
                      "where" => "id=3"
                     ));
?>

This code is free to use any application you want, but you might want to use the PEAR MDB2 class instead.
Warning: include(footer.php) [function.include]: failed to open stream: No such file or directory in /home1/taskboyc/public_html/programs/php_sql/index.php on line 52

Warning: include() [function.include]: Failed opening 'footer.php' for inclusion (include_path='/home/jjohn/sites/taskboy/www') in /home1/taskboyc/public_html/programs/php_sql/index.php on line 52