mysqli:用mysqli封装站点数据库操作功能

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)
{
($lFromRoot)
require_once("config.php");

require_once("../config.php");
  
$this->lShowErrorMsg = SITE_INFO_DEBUG;
  
($lWithDBName)
$this->oMySQLi = mysqli(DB_HOST, DB_USER, DB_PWD, DB_NAME);

$this->oMySQLi = mysqli(DB_HOST, DB_USER, DB_PWD);
  
(!$this->oMySQLi)
{
$this->BuildErrorMsg(”Connect database failed!”);
false;
}
  
//Set charactor
$this->oMySQLi->query(”SET NAMES ” . DB_CHARSET);
}
  
function __destruct
{
$this->close;
}
  
function Close
{
($this->oMySQLi)
{
($this->oQueryResult)
{
$this->oQueryResult->close;
$this->oQueryResult = false;
}
  
$this->oMySQLi->close;
$this->oMySQLi = false;
}
}
  
//Show error message
protected function BuildErrorMsg($cMsg)
{
(($cMsg “”) || (!$this->lShowErrorMsg))
{
;
}
  
$cMsg = “
  
” . $cMsg . “
  
” . $this->oMySQLi->error . “[Line: " . $this->oMySQLi->errno . "]
  
“;
  
BuildWarning($cMsg);
}
  
//GET functions
function GetQueryAffectedRows
{
$this->nQueryAffectedRows;
}
  
function GetQueryResultRows
{
$this->nQueryResultRows;
}
  
function GetQueryResultFieldCount
{
$this->nQueryResultFieldCount;
}
  
function GetAutoIncrementNumber
{
$this->oMySQLi->insert_id;
}
  
function GetErrorMsg
{
$this->oMySQLi->error;
}
  
function SelectDB($cDBName)
{
$this->oMySQLi->select_db($cDBName);
}
  
function isOK
{
($this->oMySQLi->errno 0);
}
  
// Execute a SELECT-SQL and the result as object and store them o a .gif' />
function Query2ObjectArray($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
(!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
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;
}
  
$this->nQueryResultRows ? $aResult : false;
}
  
// Execute a SELECT-SQL and the first row and store it in a .gif' />
function Query2SingleRowArray($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
(!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$aResult = $this->oQueryResult->fetch_.gif' />;
  
$this->nQueryResultRows ? $aResult : false;
}
  
// Execute a SELECT-SQL and the first row and store it o a object
function Query2SingleRowObject($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
(!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$oResult = $this->oQueryResult->fetch_object;
  
$this->nQueryResultRows ? $oResult : false;
}
  
// Execute a SELECT-SQL and store the result in a dyadic .gif' />
function Query2Array($cSQL)
{
$this->oQueryResult = $this->oMySQLi->query($cSQL);
  
(!$this->oQueryResult)
{
$this->BuildErrorMsg(”
  
Execute SQL failed!
  
n
  
SQL statment: ” . $cSQL . “
  
“);
  
$this->nQueryResultRows = 0;
$this->nQueryResultFieldCount = 0;
  
false;
}
  
$this->nQueryResultRows = $this->oQueryResult->num_rows;
$this->nQueryResultFieldCount = $this->oQueryResult->field_count;
  
$nRow = 0;
  
while($aRow = $this->oQueryResult->fetch_row)
{
($this->nQueryResultFieldCount 1)
{
$aResult[$nRow] = $aRow[0];
}

{
for ($i=0; $i<$this->nQueryResultFieldCount; $i)
{
$aResult[$nRow][$i] = $aRow[$i];
}
}
  
$nRow;
}
  
$this->nQueryResultRows ? $aResult : false;
}
  
//Execute a INSERT-SQL, UPDATE-SQL, DELETE-SQL or other SQL statement
function ExecuteSQL($cSQL)
{
($this->oMySQLi->real_query($cSQL))
{
$this->nQueryAffectedRows = $this->oMySQLi->affected_rows;
true;
}

{
$this->nQueryAffectedRows = 0;
$this->BuildErrorMsg(”Failed execute SQL command!”);
  
false;
}
}
  
}
  
?>


Tags:  mysqli和mysql phpmysqli mysqlimysql mysqli

延伸阅读

最新评论

发表评论