php mysql db connection class



<?php
 
class DB
{
    const db_name='dbname';
    const username='dbuser';
    const password='dbpass';
    const hostname='localhost';
    private static $connection=null;
 
    public static function connect()
    {
        $connection = mysql_connect(self::hostname, self::username, self::password);
        $connection or self::ethrow("unable to connect to db");
 
        $result = mysql_select_db (self::db_name, $connection);
        $result or self::ethrow("unable to select db");
 
        mysql_query("SET NAMES 'utf8'", $connection);
 
        self::$connection = &$connection;
    }
 
    private static function ethrow($msg)
    {
        throw new Exception($msg);
    }
 
    private  static function escape($str)
    {
        return is_null($str) ? 'NULL' : "'".mysql_real_escape_string($str,self::$connection)."'";
    }
 
    private static function query()
    {
        self::$connection or self::connect();
 
        $args = func_get_args();
        !empty($args) or self::ethrow("invalid call to sql");
 
        $query = array_shift($args);
        $query = str_replace("?S?", "%s", $query);//use of alternate syntax discourages people from trying %d, which wont work
        $args = array_map(array('self','escape'), $args);
        array_unshift($args, $query);
        $string = call_user_func_array('sprintf',$args);
        $result = mysql_query($string, self::$connection);
        $result or self::ethrow("an error has occurred:".mysql_error(self::$connection));
        return $result;
    }
 
    public static function sql_exec()
    {
        $args = func_get_args();
        $result = call_user_func_array(array('self','query'), $args);
        $query = trim($args[0]);
        if (preg_match('/^(update|delete)/i', $query))
        {
            return mysql_affected_rows(self::$connection);
        }
        else if (preg_match('/^(insert)/i', $query))
        {
            return mysql_insert_id(self::$connection);
        }
        return 0;
    }
 
    public static function sql_rows()
    {
        $rows = array();
        $args = func_get_args();
        $result = call_user_func_array(array('self','query'), $args);
        while ($row = mysql_fetch_assoc($result)) 
        {
            $rows[] = $row;
        }
        mysql_free_result($result);
        return $rows;
    }
 
    public static function sql_row()
    {
        $args = func_get_args();
        $result = call_user_func_array(array('self','query'), $args);
        $row = mysql_fetch_assoc($result);
        mysql_free_result($result);
        return !empty($row) ? $row : array();
    }
}
 
function sql_all()
{
    $args = func_get_args();
    return call_user_func_array(array('DB','sql_all'), $args);
}
 
function sql_row()
{
    $args = func_get_args();
    return call_user_func_array(array('DB','sql_row'), $args);
}
 
function sql_exec()
{
    $args = func_get_args();
    return call_user_func_array(array('DB','sql_exec'), $args);
}
?>



code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)