Código PHP:
class DbConn {
private $_sHost = '';
private $_sUser = '';
private $_sPass = '';
private $_sDbName = '';
private $_rConn = null;
private $_bReady = false;
function DbConn($sHost = '', $sUser = '', $sPass = '') {
$this->_sHost = $sHost;
$this->_sUser = $sUser;
$this->_sPass = $sPass;
}
function __destruct() {
$this->disconnect();
return $this->connected() === false;
}
function connect($sHost = '', $sUser = '', $sPass = '') {
if(!extension_loaded('mysql')) @dl('mysql.' . PHP_SHLIB_SUFFIX);
if(!function_exists('mysql_connect')) {
die(__CLASS__ . '::connect(): la exetensión MySQL no ha sido encontrada');
}
if($this->connected()) return true;
$this->_sHost = strlen($sHost) ? $sHost : $this->_sHost;
$this->_sUser = strlen($sUser) ? $sUser : $this->_sUser;
$this->_sPass = strlen($sPass) ? $sPass : $this->_sPass;
$this->_rConn = mysql_connect($this->_sHost, $this->_sUser, $this->_sPass);
if(!is_resource($this->_rConn)) {
@error_log(__CLASS__ . '::connect(): Error al conectar con el servidor MySQL' . mysql_error());
$this->_bReady = false;
}
$this->_bReady = true;
return $this->connected();
}
public
function disconnect() {
$this->_bReady = false;
if(!$this->connected()) return true; /* already disconnected, nothing to do */
return mysql_close($this->_rConn);
}
public
function connected() {
if(!is_resource($this->_rConn)) return false;
return (get_resource_type($this->_rConn) == 'mysql link');
}
public
function selectDb($sDbName) {
if(!$this->connected()) return false;
$this->_sDbName = $sDbName;
if(!@mysql_select_db($this->_sDbName, $this->_rConn)) {
@error_log(__CLASS__ . '::selectDB(): Error al seleccionar la base de datos' . mysql_error($this->_rConn));
$this->_sDbName = '';
$this->_bReady = false;
return false;
}
return true;
}
public
function dbSelected() {
return $this->connected() && (bool) strlen($this->_sDbName);
}
public
function query($sQuery) {
if(!$this->_bReady || !$this->connected()) return false;
$rRes = @mysql_query($sQuery, $this->_rConn);
if(!is_resource($rRes) && mysql_errno() > 0) {
$sErr = mysql_error($this->_rConn);
$iErrCode = mysql_errno($this->_rConn);
@error_log(__CLASS__ . "::query(): Error: {$sErr} ({$iErrCode})");
}
return $rRes;
}
public
function sanitize($sString) {
return str_replace('\'', '\'\'', $sString);
}
static
function isResult($rRes) {
if(!is_resource($rRes)) return false;
return (get_resource_type($rRes) == 'mysql result');
}
static
function numRows($rRes) {
if(!DbConn::isResult($rRes)) return null;
return mysql_num_rows($rRes);
}
function affectedRows() {
if(!$this->connected()) return null;
return mysql_affected_rows($this->_rConn);
}
static
function numFields($rRes) {
if(!DbConn::isResult($rRes)) return null;
return mysql_num_fields ($rRes);
}
function maxID($tabla,$id){
if(!$this->connected()) return null;
$query = 'SELECT MAX('.$id.') AS last_id FROM '.$tabla;
$result = mysql_query($query);
$result = mysql_fetch_array($result);
return $result[last_id]+1;
}
function insertId() {
if(!$this->connected()) return null;
return mysql_insert_id($this->_rConn);
}
static
function fetchArray($rRes) {
if(!self::isResult($rRes)) return null;
return mysql_fetch_array($rRes);
}
static
function fetchLengths($rRes) {
if(!self::isResult($rRes)) return null;
return mysql_fetch_lengths($rRes);
}
static
function fetchObject($rRes) {
if(!self::isResult($rRes)) return null;
return mysql_fetch_object($rRes);
}
static
function fetchRow($rRes) {
if(!self::isResult($rRes)) return null;
return mysql_fetch_row($rRes);
}
static
function getAllRows($rRes) {
if(!DbConn::isResult($rRes)) return null;
if(!mysql_num_rows($rRes)) return array();
mysql_data_seek($rRes, 0);
$aRet = array();
while($aRow = self::fetchArray($rRes)) {
$aRet[] = $aRow;
}
return $aRet;
}
}