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=
参考代码:
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;
}
}
}
?>