【问题标题】:Mysqli converted class not closing and workingMysqli 转换类没有关闭和工作
【发布时间】: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 接口是完全多余的。
  • 对我有什么更正吗?

标签: php mysql class mysqli


【解决方案1】:

第一个错误:

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

如果您在尝试连接到 mysqld 服务器时收到 Too many connections 错误,这意味着所有可用连接都被其他客户端使用。 source

您在此数据库上还有其他服务吗?其他服务似乎使用了所有可用的连接。

第二个错误:

PHP 警告:mysqli_close() 期望参数 1 为 mysqli,布尔值在 /home/account/public_html/library/class.db.blog.php 第 152 行给出

不用担心这个。当尝试打开下一个连接并达到连接限制时,mysqli_connection 将返回 false 值。比脚本正在尝试 mysqli_close(false);当您解决第一个问题时,第二个问题也将得到解决。

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多