PHOBOSLAB

Blog Home

Static MySQL Class

This Class assumes you have defined the following constants somewhere before loading and using it:

define( 'DB_HOST',         'localhost');
define( 'DB_DATABASE',     'pagenode');
define( 'DB_USER',         'root');
define( 'DB_PASSWORD',     'zomfg');

After including the DB class you can use it like this:

$foo = MySQL::query( 'SELECT id, name FROM table WHERE x = :1', 'bar' );
foreach( $foo as $row ) {
    echo $row['name'];
}

There is no need to explicitly connect to the database, as the class connects as soon as it needs to - i.e. on the first query.

The class also provides easy methods to insert a new row into a table, or update an existing one:

// Insert a new row with id 42 and name "foo"
MySQL::insertRow( 'table_name', array( 'id' => 42, 'name' => 'foo' ) );

// Set the name to "bar" where the id equals 42
MySQL::updateRow( 'table_name', array( 'id' => 42 ), array( 'name' => 'bar' ) );

There are some more methods which should be self explanatory. So, here's the source. Do with it whatever you want. A link back to my site would be nice, though :)

class MySQL {
    private static $link = null;
    private static $result;
    public static $sql;
    public static $numQueries = 0;

    private static function connect() {
        self::$link = @mysql_connect( DB_HOST, DB_USER, DB_PASSWORD )
            or die( "Couldn't establish link to database-server: ".DB_HOST );
        mysql_select_db( DB_DATABASE )
            or die( "Couldn't select Database: ".DB_DATABASE );

        mysql_query( "SET NAMES UTF8" );
    }

    public static function foundRows() {
        $r = self::query( 'SELECT FOUND_ROWS() AS foundRows' );
        return $r[0]['foundRows'];
    }

    public static function numRows() {
        return mysql_num_rows( self::$result );
    }

    public static function affectedRows() {
        return mysql_affected_rows( self::$result );
    }

    public static function insertId() {
        return mysql_insert_id( self::$link );
    }

    public static function query( $q, $params = array() ) {
        if( self::$link === null ) {
            self::connect();
        }

        if( !is_array( $params ) ) {
            $params = array_slice( func_get_args(), 1 );
        }

        if( !empty( $params ) ) {
            $q = preg_replace('/:(\d+)/e', 'self::quote($params[\\1 - 1])', $q );
        }
        self::$numQueries++;
        self::$sql = $q;
        self::$result = mysql_query( $q, self::$link );

        if( !self::$result ) {
            return false;
        }
        else if( !is_resource( self::$result ) ) {
            return true;
        }

        $rset = array();
        while ( $row = mysql_fetch_assoc( self::$result ) ) {
            $rset[] = $row;
        }
        return $rset;
    }

    public static function getRow( $q, $params = array() ) {
        if( !is_array( $params ) ) {
            $params = array_slice( func_get_args(), 1 );
        }

        $r = self::query( $q, $params );
        return array_shift( $r );
    }

    public static function updateRow( $table, $idFields, $updateFields ) {
        $updateString = implode( ',', self::quoteArray( $updateFields, true ) );
        $idString = implode( ' AND ', self::quoteArray( $idFields, true ) );
        return self::query( "UPDATE $table SET $updateString WHERE $idString" );
    }

    public static function insertRow( $table, $insertFields ) {
        $insertString = implode( ',', self::quoteArray( $insertFields, true ) );
        return self::query( "INSERT INTO $table SET $insertString" );
    }

    public static function getError() {
        if( $e = mysql_error( self::$link ) ) {
            return "MySQL reports: '$e' on query\n".self::$sql;
        }
        return false;
    }

    public static function quote( $s ) {
        if( $s === true ) {
            return 1;
        }
        else if( $s === false ) {
            return 0;
        }
        else if( is_int($s) || is_float($s) ) {
            return $s;
        }
        else {
            return "'".mysql_real_escape_string( $s )."'";
        }
    }

    public static function quoteArray( &$fields, $useKeys = false ) {
        $r = array();
        foreach( $fields as $key => &$value ) {
            $r[] = ( $useKeys ? "`$key`=":'' ) . self::quote( $value );
        }
        return $r;
    }
}