2014-01-27 31 views
0
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

任何解決方案請?

+1

你爲什麼包裝mysqli?它已經有一個OOP接口。包裝一個結構體並將mysqli對象嵌入到你的程序中是一回事,但在程序版本週圍構建一個全新的oop接口是完全多餘的。 –

+0

那麼對我的任何更正? – XaoChaos

回答

0

第一個錯誤:

PHP Warning: mysqli_connect() [function.mysqli-connect]: (08004/1040): Too many connections in /home/account/public_html/library/class.db.blog.php on line 19

如果當您嘗試連接到mysqld服務器得到一個連接太多的錯誤,這意味着所有可用的連接都在被其他客戶使用。 source

該數據庫還沒有其他服務?它看起來像其他服務使用所有可用的連接。

二錯誤:

PHP Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/account/public_html/library/class.db.blog.php on line 152

不要擔心這一點。當嘗試打開下一個連接並且達到連接限制時,mysqli_connection將返回false值。腳本正在嘗試mysqli_close(false);當你解決第一個問題時,第二個問題也會被修復。