Sunteți pe pagina 1din 2

Snippets - Simple PHP DB Classes | byteMyCode

Page 1 of 2

<?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`

8/9/2009

Snippets - Simple PHP DB Classes | byteMyCode

Page 2 of 2

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";
}
?>

8/9/2009