NotPDO for PHP: Wrapping MySQL to look like PDO

Display mode

Back to Quick Hacks

Ever been in a situation where you need PDO-MySQL installed and running, but you don't have it? If it's imperative for your script to run database queries through PDO, and it's not installed on the server, there is a way to alleviate the problem: use a wrapper that looks like PDO.

The following is just such a wrapper, that I wrote to get an application running on a new host quickly. It's a hack, and functionality is missing that would otherwise be in PDO, but it covers the basics.

NotPDO.php: PDO lookalike wrapper for MySQL

<?php

class NotPDO 
{
    private $dbconn;

    function NotPDO($dsn, $user='', $pass='')
    {
        $dsnparts = explode(':', $dsn);
        switch($dsnparts[0])
        {
            case 'mysql':
                $dsnparams = explode(';', $dsnparts[1]);
                foreach($dsnparams as $dsnp)
                {
                    $dsnpv = explode('=', $dsnp);
                    switch($dsnpv[0])
                    {
                        case 'host':
                            $host = $dsnpv[1];
                            break;
                        case 'dbname':
                            $dbname = $dsnpv[1];
                            break;
                    }
                }

                if(isset($host) && isset($dbname))
                {
                    $this->dbconn = mysql_connect($host, $user, $pass);
                    if(!$this->dbconn)
                        die('NotPDO: Database connection failed.');
                    if(!mysql_select_db($dbname, $this->dbconn))
                        die('NotPDO: Could not select database.');
                }
                else
                {
                    die('NotPDO: Database not specified.');
                }
                break;

            default:
                die('NotPDO: Database type not supported.');
        }
    }

    function prepare($q)
    {
        return new NotPDOQuery($q, $this->dbconn);
    }

    function query($q)
    {
        $q = new NotPDOQuery($q, $this->dbconn);
        $q->execute();
        return $q;
    }
};

class NotPDOQuery
{
    private $dbconn;
    private $q;
    private $r;

    function NotPDOQuery($query, $dbconn)
    {
        $this->dbconn = $dbconn;
        $this->q = $query;
    }

    function bindParam($param, $val)
    {
        if(is_numeric($val))
        {
            $this->q = str_replace(
                              $param,
                              mysql_real_escape_string($val, $this->dbconn),
                              $this->q);
        }
        else
        {
            $this->q = str_replace(
                              $param,
                              "'".mysql_real_escape_string($val, $this->dbconn)."'",
                              $this->q);
        }
    }

    function execute()
    {
        $this->r = mysql_query($this->q);
        if($this->r)
            return true;
        else
            return false;
    }

    function fetch()
    {
        return mysql_fetch_assoc($this->r);
    }

    function fetchAll()
    {
        $arr = array();
        if(mysql_num_rows($this->r)) mysql_data_seek($this->r, 0);
        while($row = mysql_fetch_assoc($this->r)) $arr[] = $row;
        return $arr;
    }
};

?>