I've been working on a PHP DB class to help simplify my MySQL DB calls, so I figured I'd see what everyone has to think & see if anyone has suggestions. You are welcome to use it any which way you like.
Here it is...
Rich
$dbConfig = array();
$dbConfig['host'] = "";
$dbConfig['name'] = "";
$dbConfig['user'] = "";
$dbConfig['pass'] = "";
class Database
{
# Set DB vars
function config($dbConfigArray)
{
$this->hostname = $dbConfigArray['host'];
$this->username = $dbConfigArray['user'];
$this->password = $dbConfigArray['pass'];
$this->database = $dbConfigArray['name'];
}
# Connect to DB
function connect($autoSelectDB = 1, $pConn = 1)
{
((bool) $pConn) ?
$this->connRes = mysql_pconnect($this->hostname, $this->username, $this->password, TRUE) :
$this->connRes = mysql_connect($this->hostname, $this->username, $this->password, TRUE);
if ($this->connRes && (bool) $autoSelectDB) $this->connRes = mysql_select_db($this->database);
return $this->connRes;
}
# Select DB
function select_db($whichDB = NULL)
{
if (!isset($whichDB))
return @mysql_select_db($this->database);
else
return @mysql_select_db($whichDB);
}
# Query DB
function query($theQry, $selectAll = 0, $fetchType = NULL)
{
((bool) $selectAll) ?
$this->qryStr = "SELECT * FROM `{$theQry}`" :
$this->qryStr = $theQry;
$this->qry = @mysql_query($this->qryStr);
switch ((string) $fetchType) {
case 'array':
return @mysql_fetch_array($this->qry);
break;
case 'assoc':
return @mysql_fetch_assoc($this->qry);
break;
default:
return $this->qry;
break;
}
}
# DB Insert Function
function insert($table, $fieldArray)
{
foreach ($fieldArray as $this->f => $this->v)
$this->ins['`'.$this->f.'`'] = '\''.$this->v.'\'';
$this->insStr = "INSERT INTO `{$table}` (".implode(', ',array_keys($this->ins)).") VALUES (".implode(', ',array_values($this->ins)).")";
return Database::query($this->insStr);
}
# DB Update Function
function update($table, $fieldArray, $idField, $id)
{
foreach($fieldArray as $this->f => $this->v)
$this->upd[] = "`{$this->f}`='{$this->v}'";
$this->updStr = "UPDATE `{$table}` SET ".implode(', ',$this->upd)." WHERE `{$idField}`={$id}";
return Database::query($this->updStr);
}
# DB Delete Function
function delete($table, $field, $id)
{
$this->delStr = "DELETE FROM `{$table}` WHERE `{$field}`='{$id}'";
return Database::query($this->delStr);
}
}
Sign In »
Register Now!
Help






















