gpt4 book ai didi

php - Mysqli 转换的类未关闭且无法工作

转载 作者:行者123 更新时间:2023-11-30 00:45:35 25 4
gpt4 key购买 nike

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): 第 19 行/home/account/public_html/library/class.db.blog.php 中的连接过多

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

请问有什么解决办法吗?

最佳答案

第一个错误:

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);当你解决第一个问题时,第二个问题也将得到解决。

关于php - Mysqli 转换的类未关闭且无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21387879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com