class dbConn {
private static $dbConn;
public function __construct()
{
try {
//$this->dbConn = new mysqli(dbHost, dbUser, dbPass, dbName) or die();
$this->dbConn = mysqli_connect(dbHost, dbUser, dbPass, dbName) or die();
//mysqli_select_db($this->dbConn, dbName) or die();
@mysqli_set_charset($this->dbConn, 'utf8');
} catch (Exception $e) {
die($e->getMessage());
}
}
public static function singleton()
{
if(!isset(self::$dbConn)) {
$c = __CLASS__;
self::$dbConn = new $c;
}
return self::$dbConn;
}
public function query($sql)
{
try {
if(!$q = mysqli_query($this->dbConn, $sql))
throw new Exception($this->debug($sql));
} catch (Exception $e) {
die($e->getMessage());
}
return $q;
}
public function getValue($sql)
{
$row = mysqli_fetch_row($this->query($sql));
return $row[0];
}
public function getRow($sql, $cache = 1)
{
$res = mysqli_fetch_assoc($this->query($sql));
return $res;
}
public function getRows($sql, $cache = 1)
{
$q = $this->query($sql);
while($row = mysqli_fetch_assoc($q))
$res[] = $row;
return $res;
}
public function getLastInsertId() {
return mysqli_insert_id($this->dbConn);
}
public function getRowNum($sql)
{
$res = mysqli_num_rows($this->query($sql));
return $res;
}
/* Escape */
public function varEscape()
{
if($_GET) $_GET = $this->arrayEscape($_GET);
if($_POST) $_POST = $this->arrayEscape($_POST);
if($_COOKIE) $_COOKIE = $this->arrayEscape($_COOKIE);
if($_REQUEST) $_REQUEST = $this->arrayEscape($_REQUEST);
}
public function arrayEscape($arr)
{
foreach($arr as $key => $val)
$arr[$key] = is_array($val) ? $this->arrayEscape($val) : $this->strEscape($val);
return $arr;
}
public function strEscape($str)
{
if(get_magic_quotes_gpc())
$str = stripslashes($str);
$str = trim($str);
$str = $this->strControl($str);
$str = mysqli_real_escape_string($this->dbConn, $str);
return $str;
}
public function strControl($str)
{
$s = array('/*', '*/', 'UNION', 'NULL', '<!--', '-->');
$r = array('', '', '', '', '', '');
return $str = str_replace($s, $r, $str);
}
public function debug($sql = 'N/A')
{
if(DEBUG) {
$str = '<b>Debug Mode!</b>'
. '<br />Referer: ' . $_SERVER['HTTP_REFERER']
. '<br />File: ' . $_SERVER['PHP_SELF']
. '<br />Error: ' . mysqli_error($this->dbConn)
. '<br />SQL: ' . $sql;
foreach($_GET as $key => $val)
$str .= '<br />GET: ' . $key . ' = ' . $val;
foreach($_POST as $key => $val)
$str .= '<br />POST: ' . $key . ' = ' . (is_array($val) ? print_r($val, true) : $val);
foreach($_FILES as $key => $val)
$str .= '<br />FILES: ' . $key . ' = ' . (is_array($val) ? print_r($val, true) : $val);
foreach($_SESSION as $key => $val)
$str .= '<br />SESSION: ' . $key . ' = ' . (is_array($val) ? print_r($val, true) : $val);
$css = 'font-family: Arial; font-size: 12px; color: red;';
$str = '<pre style="' . $css . '">' . $str . '</pre>';
}
return $str;
}
public function __destruct() {
mysqli_close($this->dbConn);
}
}
這是我的代碼連接和其他查詢人員。 現在,它給了我一樣的錯誤:Mysqli轉換類不關閉和工作
PHP的警告:mysqli_connect()[function.mysqli-連接]:(08004/1040):在/home/account/public_html/library/class.db.blog連接太多.PHP第19行
PHP警告:mysqli_close()預計參數1是mysqli的,在給定的/home/account/public_html/library/class.db.blog.php上線布爾152
任何解決方案請?
你爲什麼包裝mysqli?它已經有一個OOP接口。包裝一個結構體並將mysqli對象嵌入到你的程序中是一回事,但在程序版本週圍構建一個全新的oop接口是完全多餘的。 –
那麼對我的任何更正? – XaoChaos