您的位置 >>> 星想互联 >>> 编程技术 >>> PHP高级编程
PHP数据库操作类
点击数:1815  发布时间2017-08-02 23:34:16

/**
* 数据库类
* 说明:系统底层数据库核心类
*      调用这个类前,请先设定这些外部变量
*      $GLOBALS['db_host'];
*      $GLOBALS['db_user'];
*      $GLOBALS['db_pwd'];
*      $GLOBALS['db_name'];
*      $GLOBALS['db_tablepre'];
*
*/
// 在系统所有文件中均不需要单独初始化这个类
// 可直接用 $dosql 或 $db 进行操作
// 为了防止错误,操作完后不必关闭数据库
$dosql = $db = new MySql(false);

class MySql
{
var $db_host;
var $db_user;
var $db_pwd;
var $db_name;
var $db_tablepre;

var $linkid;
var $result;
var $querystring;
var $isclose;
var $safecheck;


//用外部定义的变量初始类,并连接数据库
function __construct($pconnect=false, $nconnect=true)
{
$this->isclose   = false;
$this->safecheck = true;
if($nconnect)
{
$this->Init($pconnect);
}
}


//pconnect为长连接,nconnect为短连接
function MySql($pconnect=false, $nconnect=true)
{
$this->__construct($pconnect, $nconnect);
}

function Init($pconnect=false)
{
$this->db_host      = $GLOBALS['db_host'];
$this->db_user      = $GLOBALS['db_user'];
$this->db_pwd       = $GLOBALS['db_pwd'];
$this->db_name      = $GLOBALS['db_name'];
//$this->db_tablepre  = $GLOBALS['db_tablepre'];
$this->linkid       = 0;
$this->result['me'] = 0;
$this->querystring  = '';
$this->Open($pconnect);
}


//连接数据库
function Open($pconnect=false)
{
global $dosql;

//连接数据库
if($dosql && !$dosql->isclose)
{
$this->linkid = $dosql->linkid;
}
else
{
$i = 0;
while(!$this->linkid)
{
if ($i > 100) break;

if(!$pconnect)
{
$this->linkid = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
else
{
$this->linkid = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
}
$i++;
}
}

//处理错误,成功连接则选择数据库
if(!$this->linkid)
{
$this->DisplayError("错误警告:连接数据库失败,可能数据库密码不对或数据库服务器出错!");
exit();
}

if(!@mysql_select_db($this->db_name))
{
$this->DisplayError('无法使用数据库');
exit();
}

$mysqlver = explode('.',$this->GetVersion());
$mysqlver = $mysqlver[0].'.'.$mysqlver[1];

if($mysqlver > 4.0)
{
@mysql_query("SET NAMES '".$GLOBALS['db_charset']."', character_set_client=binary, sql_mode='', interactive_timeout=3600;", $this->linkid);
}
return true;
}

//-------------------------------------------------------------------------------------------------
//执行一个不返回结果的SQL语句,如update,delete,insert等
function ExecNoneQuery($sql='')
{
global $dosql;
if($dosql->isclose)
{
$this->Open(false);
$dosql->isclose = false;
}

if(!empty($sql))
{
$this->SetQuery($sql);
}
else
{
return false;
}

//SQL语句安全检查
if($this->safecheck)
{
$this->CheckSql($this->querystring,'update');
}

if(mysql_query($this->querystring, $this->linkid))
{
return true;
}
else
{
$this->DisplayError(mysql_error().'Error sql:'.$this->querystring);
exit();
}
}

//执行一个带返回结果的SQL语句,如SELECT,SHOW等 $id的用途在于区别每个记录集
function Execute($sql, $id="me")
{
global $dosql;
if($dosql->isclose)
{
$this->Open(false);
$dosql->isclose = false;
}

if(!empty($sql))
{
$this->SetQuery($sql);
}
else
{
return false;
}

//SQL语句安全检查
if($this->safecheck)
{
$this->CheckSql($this->querystring);
}

$this->result[$id] = mysql_query($this->querystring, $this->linkid);

if(empty($this->result[$id]) && $this->result[$id]===false)
{
$this->DisplayError(mysql_error().'Error sql:'.$this->querystring);
exit();
}
}

//执行一个不与任何表名有关的SQL语句,Create等
function ExecuteSafeQuery($sql,$id="me")
{
global $dosql;
if($dosql->isclose)
{
$this->Open(false);
$dosql->isclose = false;
}
$this->result[$id] = @mysql_query($sql,$this->linkid);
}

//执行一个SQL语句,返回前一条记录或仅返回一条记录
function GetOne($sql='', $acctype=MYSQL_ASSOC)
{
global $dosql;
if($dosql->isclose)
{
$this->Open(false);
$dosql->isclose = false;
}
if(!empty($sql))
{
if(!preg_match("/LIMIT/i", $sql)) $this->SetQuery(preg_replace("/[,;]$/i", '', trim($sql))." LIMIT 0,1;");
else $this->SetQuery($sql);
}
$this->Execute($sql,"one");
$arr = $this->GetArray("one", $acctype);
if(!is_array($arr))
{
return '';
}
else
{
@mysql_free_result($this->result["one"]); return($arr);
}
}

//返回当前的一条记录并把游标移向下一记录
// MYSQL_ASSOC、MYSQL_NUM、MYSQL_BOTH
function GetArray($id="me",$acctype=MYSQL_ASSOC)
{
if($this->result[$id]==0)
{
return false;
}
else
{
return mysql_fetch_array($this->result[$id],$acctype);
}
}

function GetObject($id="me")
{
if($this->result[$id]==0)
{
return false;
}
else
{
return mysql_fetch_object($this->result[$id]);
}
}


}

来源:星想互联