My own database abstraction class

Back in 2006 July, I decided that all other database classes are not worth it, and created my own one, incorporating best features from MySQL and PHP world. It resulted in this brilliant code, which I showed to few colleagues, and got such quote:

i like your nonframework. it gives a fuzzy feeling to the poor souls that think they need an abstraction layer. — Kristian Köhntopp

This was written using TIC pattern and can be used in variety of applications:

class MyDB {
  var $conn = null;

  function MyDB($database=null,$user='root',
                $password='',$host='localhost') {
    $this->conn=mysql_connect($host,$user,$password) and
    $database?mysql_select_db($database, $this->conn):null;

  function _escape($s) {
    return mysql_real_escape_string($s,$this->conn);

  function _quote($s) {
    return "'" . $this->_escape($s) . "'";

  function __call($method,$arguments) {
         'return " ".strtolower($s[0]);'),
    $query=str_replace(' everything ',' * ',$query);
    if ($first) {
      if (is_array($first)) {
        $query .= ' (' . implode(',',
                            $first)) . ') ';
      } else {
        while($argument = array_shift($arguments)) {
          $first = preg_replace('/\?/',
        $query .= $first;
    if (!$res) { print mysql_error(); exit(); }
    while($row=mysql_fetch_assoc($res)) {
    return $ret;


$x = new MyDB('test');
$x->selectEverythingFromMytableWhereIdIn(" (?,?,?) ","a'b",2,3);

Now I wonder where I should build community for this, Google Code or Sourceforge? Or should that be the darling MySQL Forge.

As I started digging my stuff from history, I also managed to upload my past six month pictures to flickr too – including Vienna, Taipei and few other trips.

10 thoughts on “My own database abstraction class”

  1. Try replace function MyDB to
    public function __construct($db = null,..

    function MyDB is php4 construct, not php5

  2. Yeah, your right. Most DB abstraction libraries out there use up more memory then the rest of a CMS’s code when rendering a page.

    Take pear DB, it uses about 300KB, and MDB2 around 800KB to include the class!

  3. Please, don’t even try to build a “community”. The class is awful. I don’t even want to image how complex queries would look like..

  4. Does your API support sub selects and union ? You also forgot to optimize your code with ‘//’.

  5. rails (active record) has this for years. i have to work with some php coders, and it’s funny to look at their opinions about this class, sorry, but some hardcore php guys just looks so lame with their inside box, phu, sorry inside php thinking.

    about lib, its ok, keep working on it.

Comments are closed.