Simple PHP DB Classes





17
Date Submitted Mon. Oct. 9th, 2006 3:13 PM
Revision 1 of 1
Helper inxilpro
Tags classes | Database | DB | PHP | Simple
Comments 6 comments
This is a bare-bones DB connectivity class. It's really just meant to abstract a few basic mysql_ functions so that if you ever have to change DBs you don't have to change every database function call.

I really just threw this together on a whim with no testing what-so-ever. Also, it only abstracts the most basic MySQL functions--this is intentional; please expand it as you see fit.

<?php
class SimpleDB_Connection
{
        var $conn;
        var $error = 'No Error';
       
        function SimpleDB_Connection($hostname = 'localhost', $username = null, $password = null)
        {
                $this->conn = @mysql_connect($hostname, $username, $password);
                if (!$this->conn) $this->error = 'Could not connect to database.';
                return $this->conn;
        }
       
        function query($sql)
        {
                if (!$this->conn)
                {
                        $this->error = 'Not connected to database.';
                        return false;
                }
               
                $res = @mysql_query($sql, $this->conn);
                if (!$res)
                {
                        $this->error = mysql_error($this->conn);
                        return false;
                }
               
                return new SimpleDB_Result($res, $this->conn);   
        }
       
        function getError()
        {
                return $this->error;
        }
}

class SimpleDB_Result
{
        var $conn;
        var $res;
        var $numRows;
        var $numAffected;
       
        function SimpleDB_Result(&$res, &$conn)
        {
                $this->res = $res;
                $this->conn = $conn;
        }
       
        function getNext()
        {
                $res = @mysql_fetch_assoc($this->res);
                return $res;
        }
       
        function getNumRows()
        {
                $res = ($this->numRows ? $this->numRows : @mysql_num_rows($this->res));
                return $res;
        }
       
        function getNumAffected()
        {
                $res = ($this->numAffected ? $this->numAffected : @mysql_affected_rows($this->conn));
                return $res;
        }
}

/**
 * EXAMPLES:
 */


$db = new SimpleDB_Connection('localhost', 'username', 'password');
$sql = 'SELECT *
                FROM `users`
                WHERE `userid` < 99'
;
$result = $db->query($sql);
if (!$result) die($db->getError());
if ($result->getNumRows() == 0) die('No Results');
while ($row = $result->getNext())
{
        echo "{$row['username']}<br />\n";
}
?>
 

Chris M

Comments

Comments Query arguments
Sat. Oct. 28th, 2006 4:12 PM    Scripter SCoon
Comments result class
Mon. Oct. 9th, 2006 3:23 PM    Helper BrandonReese
  Comments Thanks
Mon. Oct. 9th, 2006 3:42 PM    Helper inxilpro
Comments Revision 2
Mon. Oct. 9th, 2006 3:46 PM    Helper inxilpro
Comments Good job
Tue. Sep. 4th, 2007 7:09 PM    Helper explode
Comments Nice one!
Sat. Feb. 11th, 2012 1:04 PM    Newbie mlee3680

Voting