Simple PHP DB Classes
15
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.
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";
}
?>






I see no use for this class in real projects.
<?
$numrows = $db->sql_numrows($result);
// as apposed to
$numrows = $result->numrows();
?>
I think I prefer your method.
<?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));
$this->numRows = $res;
return $res;
}
function getNumAffected()
{
$res = ($this->numAffected ? $this->numAffected : @mysql_affected_rows($this->conn));
$this->numAffected = $res;
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 Gmyr
www.chrisgmyr.com
www.syracusecs.com
www.syracusebands.net