PHP Database Abstraction Layer

If you're hunting around for a basic database abstraction layer for PHP/MySQl then here's one I found and tweaked a little that should work in php4


<?php
class database {
    var $db;
    var $query;

    function database ( $host, $user, $pass, $db ) {
        $this->db=mysql_connect($host,$user,$pass);
        mysql_select_db($db,$this->db);
    }

    function query($sql) {
        $this->query = mysql_query($sql,$this->db);
    }

    function getRow () {
        if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) )
            return $row;
        else
            return false;
    }
}
?>
<?php
//it goes something like this
$db = new database();
$sql = \"SELECT * FROM mytable\";
$db->query($sql);
$row = $db->getRow();
?>

Enjoy! I'll write a php5 database abstraction layer soon...




Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options