gpt4 book ai didi

php - 数据库错误 : [mysqli. mysqli]:用户已经有超过 'max_user_connections' 个事件连接

转载 作者:行者123 更新时间:2023-12-01 00:03:12 25 4
gpt4 key购买 nike

我有一个每天只有大约 100 人访问的站点,但是当我以用户身份登录时收到此错误消息:

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User mexautos_Juan already has more than 'max_user_connections' active connections in /home/mexautos/public_html/kiubbo/data/model.php on line 26

Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in /home/mexautos/public_html/kiubbo/data/model.php on line 87
Query failed

我刷新了几次页面,现在一切正常,但由于我没有那么多用户,我怀疑是我的代码中的错误,我应该在哪里寻找它?

谢谢

编辑:这是模型文件:

<?php
/*

Model is the base class from which the other
model classes will be derived. It offers basic
functionality to access databases

*/
require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
require_once(SITE_ROOT.'includes/exceptions.php');

class Model {

private $created;
private $modified;

static function getConnection()
{
/*

Connect to the database and return a
connection or null on failure

*/

$db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(!$db) {
echo mysql_error();
throw new Exception('Could not connect to database', EX_NO_DATABASE_CONNECTION);
}

return $db;

}

static function execSQL($query)
{
/*
Execute a SQL query on the database
passing the tablename and the sql query.
Returns the resultset
*/


$db = null;
$results = null;
//echo "query is $query";

try
{
$db = Model::getConnection();
$results = $db->query($query);
if(!$results) {
throw new Exception('Query failed', EX_QUERY_FAILED );
}
}
catch(Exception $e)
{
/* errors are handled higher in the
object hierarchy
*/

throw $e;
}

Model::closeConnection($db);

return $results;
}

static function execSQl2($query)
{
/*
Execute a SQL query on the database
passing the tablename and the sql query.
Returns the LAST_INSERT_ID
*/


$db = null;
$lastid = null;
//echo "query is $query";

try
{
$db = Model::getConnection();
$results = $db->query($query);
if(!$results) {
throw new Exception('Query failed', EX_QUERY_FAILED );
}
$lastid = $db->insert_id;
}
catch(Exception $e)
{
/* errors are handled higher in the
object hierarchy
*/

throw $e;
}

Model::closeConnection($db);

return $lastid;
}

function delete($table, $id, $conditions = '')
{
$query = "delete from $table where id = $id";
try
{
$db = Model::getConnection();
$results = Model::execSQL($query);
if(!$results){
throw new Exception('Could not delete this object', EX_DELETE_ERROR);
}
return $results->num_rows;
}

catch(Exception $e)
{
throw $e;
}
}

static function closeConnection($db)
{
$db->close();
}

function getCreated()
{
return $this->created;
}

function setCreated($value)
{
$this->created = $value;
}

function getModified()
{
return $this->modified;
}

function setModified($value)
{
$this->modified = $value;
}



}

?>

最佳答案

您网站上打开数据库连接的每次点击都使用相同的数据库用户名和密码。您的数据库设置限制了每个用户的连接数,而您正在超过该最大值。

查看此 MySQL 手册页:http://dev.mysql.com/doc/refman/5.0/en/user-resources.html

您的模型类不是很好,因为它似乎在每个单独的查询上打开和关闭数据库连接。这是一种非常糟糕的资源使用,因为打开和关闭连接的代价很高。我会在你的模型对象上编写一个名为 $db->close() 的析构函数,并更改 getConnection() 以打开一次连接,然后每次都返回它。这意味着将您的模型类转换为非静态用法,但在数据库上这样做会容易得多。

无论如何,在您的类正在执行的所有连接和断开连接操作中,MySQL 都有连接备份并且在您达到最大用户限制之前它们的清除速度不够快。

关于php - 数据库错误 : [mysqli. mysqli]:用户已经有超过 'max_user_connections' 个事件连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/777492/

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