【发布时间】:2014-01-27 17:44:50
【问题描述】:
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);
}
}
这是我用于连接和其他查询人员的代码。 现在它给了我这样的错误:
PHP 警告:mysqli_connect() [function.mysqli-connect]: (08004/1040): Too many connections in /home/account/public_html/library/class.db.blog.php on line 19
PHP 警告:mysqli_close() 期望参数 1 为 mysqli,布尔值在 /home/account/public_html/library/class.db.blog.php 第 152 行给出
请问有什么解决办法吗?
【问题讨论】:
-
你为什么要包装mysqli?它已经有一个 OOP 接口。包装一个结构并将 mysqli 对象嵌入你的结构是一回事,但围绕程序版本构建一个全新的 oop 接口是完全多余的。
-
对我有什么更正吗?