首页 新闻 会员 周边 捐助

Mysqli 的数据库操作类

0
悬赏园豆:10 [已关闭问题]

Mysqli 的数据库操作类怎么个写法呢,不是很习惯。

Wamei的主页 Wamei | 初学一级 | 园豆:170
提问于:2010-03-01 20:42
< >
分享
其他回答(1)
0

http://www.google.com/search?hl=zh-CN&newwindow=1&q=mysqli%E7%B1%BB&btnG=Google+%E6%90%9C%E7%B4%A2&lr=&aq=f&oq=

邀月 | 园豆:25475 (高人七级) | 2010-03-02 09:23
这个答案很帅气
支持(0) 反对(0) 西越泽 | 园豆:10775 (专家六级) | 2010-03-02 12:44
有点帅
支持(0) 反对(0) Wamei | 园豆:170 (初学一级) | 2010-03-04 17:34
0

参考代码:

class SiteDB
{
protected $oMySQLi;
protected $oQueryResult;
  
protected $nQueryAffectedRows = 0;
protected $nQueryResultRows = 0;
protected $nQueryResultFieldCount = 0;
  
//Variables for debug
protected $lShowErrorMsg = false;
  
function __construct($lWithDBName=true, $lFromRoot=false)
{
if ($lFromRoot)
require_once("config.php");
else
require_once("../config.php");
  
$this->lShowErrorMsg = SITE_INFO_DEBUG;
  
if($lWithDBName)
$this->oMySQLi = new mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);
else
$this->oMySQLi = new mysqli(DB_HOST, DB_USER, DB_PWD);
  
if(!$this->oMySQLi)
{
$this->BuildErrorMsg(”Connect database failed!”);
return false;
}
  
//Set charactor set
$this->oMySQLi->query(”SET NAMES ” . DB_CHARSET);
}
  
function __destruct()
{
$this->close();
}
  
function Close()
{
if($this->oMySQLi)
{
if($this->oQueryResult)
{
$this->oQueryResult->close();
$this->oQueryResult = false;
}
  
$this->oMySQLi->close();
$this->oMySQLi = false;
}
}
  
//Show error message
protected function BuildErrorMsg($cMsg)
{
if (($cMsg == “”) || (!$this->lShowErrorMsg))
{
return;
}
  
$cMsg = “
  
” . $cMsg . “
  
” . $this->oMySQLi->error . “[Line: " . $this->oMySQLi->errno . "]
  
“;
  
BuildWarning($cMsg);
}
  
//GET functions
function GetQueryAffectedRows()
{
return $this->nQueryAffectedRows;
}
  
function GetQueryResultRows()
{
return $this->nQueryResultRows;
}
  
function GetQueryResultFieldCount()
{
return $this->nQueryResultFieldCount;
}
  
function GetAutoIncrementNumber()
{
return $this->oMySQLi->insert_id;
}
  
function GetErrorMsg()
{
return $this->oMySQLi->error;
}
  
function SelectDB($cDBName)
{
$this->oMySQLi->select_db($cDBName);
}
  
function isOK()
{
return ($this->oMySQLi->errno == 0);
}
  
// Execute a SELECT-SQL and return the result as object and store them into a array
function Query2ObjectArray($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
if (!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
return false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$nRow = 0;
  
while($oRow = $this->oQueryResult->fetch_object())
{
$aResult[$nRow] = $oRow;
$nRow++;
}
  
return $this->nQueryResultRows ? $aResult : false;
}
  
// Execute a SELECT-SQL and return the first row and store it in a array
function Query2SingleRowArray($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
if (!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
return false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$aResult = $this->oQueryResult->fetch_array();
  
return $this->nQueryResultRows ? $aResult : false;
}
  
// Execute a SELECT-SQL and return the first row and store it into a object
function Query2SingleRowObject($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
if (!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
return false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$oResult = $this->oQueryResult->fetch_object();
  
return $this->nQueryResultRows ? $oResult : false;
}
  
// Execute a SELECT-SQL and store the result in a dyadic array
function Query2Array($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
if (!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
return false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$nRow = 0;
  
while($aRow = $this->oQueryResult->fetch_row())
{
if ($this->nQueryResultFieldCount == 1)
{
$aResult[$nRow] = $aRow[0];
}
else
{
for ($i=0; $i<$this->nQueryResultFieldCount; $i++)
{
$aResult[$nRow][$i] = $aRow[$i];
}
}
  
$nRow++;
}
  
return $this->nQueryResultRows ? $aResult : false;
}
  
//Execute a INSERT-SQL, UPDATE-SQL, DELETE-SQL or other SQL statement
function ExecuteSQL($cSQL)
{
if($this->oMySQLi->real_query($cSQL))
{
$this->nQueryAffectedRows = $this->oMySQLi->affected_rows;
return true;
}
else
{
$this->nQueryAffectedRows = 0;
$this->BuildErrorMsg(”Failed execute SQL command!”);
  
return false;
}
}
  
}
  
?>

风影极光 | 园豆:1573 (小虾三级) | 2010-03-02 09:56
嘿嘿,谢谢了 可以用的人不是很多,找不到很合适的
支持(0) 反对(0) Wamei | 园豆:170 (初学一级) | 2010-03-04 17:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册