MYSQLi 错误:用户已经有多个“max_user_connections"活动连接 [英] MYSQLi error: User already has more than 'max_user_connections' active connections

查看:25
本文介绍了MYSQLi 错误:用户已经有多个“max_user_connections"活动连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行的网站上出现以下错误.我不明白为什么会这样,因为它在我的本地主机上运行良好.跟楼主有关系吗?我在 Unix 服务器上.

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): 用户 dbo343879423 在/homepages/9/d322397966/htdocs/dump/models 中已经有超过 'max_user_connections' 的活动连接/class_database.php 第 11 行连接失败:用户 dbo343879423 已经有超过 'max_user_connections' 的活动连接警告:mysqli::close() [mysqli.close]:无法在第 160 行的/homepages/9/d322397966/htdocs/dump/models/class_database.php 中获取 mysqli

错误显示用户 dbo343879423 在第 11 行的/homepages/9/d322397966/htdocs/dump/models/class_database.php 中已经有超过 'max_user_connections' 的活动连接",所以这是脚本中的第 11 行 -我看不出有什么不妥!

$this ->connection = new mysqli($hostname,$username,$password,$database);

下面是class_database.php中的整个类,是不是脚本的其他部分有问题,我应该更改吗?

connection = new mysqli($hostname,$username,$password,$database);如果 (mysqli_connect_errno()){printf("连接失败:%s\n", mysqli_connect_error());出口();}}#获取所有结果行作为关联数组、数字数组或两者兼而有之公共函数 fetch_all($query){$result = $this ->连接 ->查询($查询);如果($结果){返回 $result ->fetch_all(MYSQLI_ASSOC);}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#获取结果行作为关联数组、数字数组或两者公共函数 fetch_assoc_while($query){$result = $this ->连接 ->查询($查询);如果($结果){while($row = $result -> fetch_assoc()){$return_this[] = $row;}如果(isset($return_this)){返回 $return_this;}别的{返回假;}}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#获取结果行作为关联数组公共函数 fetch_assoc($query){$result = $this ->连接 ->查询($查询);如果($结果){返回 $result ->fetch_assoc();}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#获取结果行作为枚举数组公共函数 fetch_row($query){$result = $this ->连接 ->查询($查询);如果($结果){返回 $result ->fetch_row();}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#获取结果中的行数公共函数 num_rows($query){$result = $this ->连接 ->查询($查询);如果($结果){返回 $result ->num_rows;}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#对数据库执行查询公共函数查询($query){$result = $this ->连接 ->查询($查询);如果($结果){返回 $result;}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#转义字符串中的特殊字符以用于 SQL 语句,同时考虑连接的当前字符集公共函数 real_escape_string($string){$result = $this ->连接 ->real_escape_string($string);如果($结果){返回 $result;}别的{$this ->错误 = $this ->连接 ->错误;返回假;}}#显示错误公共函数 get_error(){返回 $this ->错误;}#当对象被销毁时关闭数据库连接.公共函数 __destruct(){$this ->连接 ->关闭();}}?>

还是我应该永久更换主机!??

下面是数据库连接类的实现.如果我把这部分去掉,错误就不会再出现了,但我在网站的其他部分也这样做了,它们不会造成任何问题!

<div id="side-video-library" class="round-corner"><h4><a href="<?php echo HTTP_ROOT;?>videos"><span>ENER 视频库</span></a></h4><?php$sql = "选择 *FROM root_pages哪里 root_pages.parent_id = '8'AND root_pages.pg_highlight = '1'AND root_pages.pg_hide !='1'ORDER BY rand() DESC限制 1";#实例化__database类的对象$object_item = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);$item = $object_item ->fetch_assoc($sql);#实例化__database类的对象$object_item_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);$total_item = $object_item_num ->num_rows($sql);//echo $total_item;?><?php如果 ($total_item > 0){$sql = "选择 *FROM root_taggedLEFT JOIN root_tags ON ( root_tags.tag_id = root_tagged.tag_id )WHERE root_tagged.pg_id = '".$item['pg_id']."'";#实例化__database类的对象$object_tagname = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);$item_tagname = $object_tagname ->fetch_assoc($sql);#实例化__database类的对象$object_tagname_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);$total_tagname = $object_tagname_num ->num_rows($sql);?><p class="item-video"><对象样式=宽度:183像素;高度:151像素;"width="183" height="151" data="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>"type="application/x-shockwave-flash"><param name="wmode" value="transparent"/><param name="src" value="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>"/></对象></p><h3><a href="<?php echo HTTP_ROOT.str_replace(' ', '-', 'videos').'/'.$item_tagname['tag_name'].'/'.str_replace('', '-', strtolower($item['pg_url']));?>"><?php if(strlen($item['pg_title']) > 20) echo substr($item['pg_title'], 0,20).'...';else echo $item['pg_title'];?></a></h3><p class="item-excerpt-video"><?php if(strlen($item['pg_content_2']) > 100) echo substr($item['pg_content_2'], 0,100).'...';else echo $item['pg_content_2'];?></p><a href="<?php echo HTTP_ROOT;?>videos" class="button-arrow"><span>更多</span></a><?php}?>

<!-- 侧视频库-->

我是否错误地实现了该类??

谢谢.

解决方案

问题可能在于您只允许少数几个连接,而当您的班级尝试获取新连接时,您会遇到此错误.

这不是编程问题,只是可用资源的数量.任何其他使用此类的脚本都会出现错误.

您必须在服务器上的 mysql 配置文件上配置更多连接.如果您没有此访​​问权限,请要求支持人员执行此操作或更改为允许更多连接的托管公司!

另一种选择是在这个类上实现一个单例模式,这样它就可以重用相同的连接池,并且不会突破限制.

I have this error below on a site I am running. I don't understand why is that as it works fine on my localhost. Is it something to do with the host? I am on an Unix server.

Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1203): User dbo343879423 already has more than 'max_user_connections' active connections in /homepages/9/d322397966/htdocs/dump/models/class_database.php on line 11
Connect failed: User dbo343879423 already has more than 'max_user_connections' active connections 
Warning: mysqli::close() [mysqli.close]: Couldn't fetch mysqli in /homepages/9/d322397966/htdocs/dump/models/class_database.php on line 160

the error says 'User dbo343879423 already has more than 'max_user_connections' active connections in /homepages/9/d322397966/htdocs/dump/models/class_database.php on line 11', so this is the line 11 in the script - I can't see anything wrong!

$this -> connection = new mysqli($hostname,$username,$password,$database);

below is the entire class in class_database.php, is it wrong in other part of script and I should change?

<?php
#connects the database and handling the result
class __database {

    protected $connection = null;
    protected $error = null;

    #make a connection
    public function __construct($hostname,$username,$password,$database)
    {
        $this -> connection = new mysqli($hostname,$username,$password,$database);

        if (mysqli_connect_errno()) 
        {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }
    }

    #fetches all result rows as an associative array, a numeric array, or both
    public function fetch_all($query) 
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_all(MYSQLI_ASSOC);
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #fetches a result row as an associative array, a numeric array, or both
    public function fetch_assoc_while($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            while($row = $result -> fetch_assoc())
            {
                $return_this[] = $row;
            }

            if (isset($return_this))
            {
                return $return_this;
            }
            else
            {
                return false;
            }
        }
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #fetch a result row as an associative array
    public function fetch_assoc($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_assoc();
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #get a result row as an enumerated array
    public function fetch_row($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> fetch_row();
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #get the number of rows in a result
    public function num_rows($query)
    {
        $result = $this -> connection -> query($query);
        if($result) 
        {
            return $result -> num_rows;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }
    }

    #performs a query on the database
    public function query($query)
    {
        $result = $this -> connection -> query($query); 
        if($result) 
        {
            return $result;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }

    }

    #escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
    public function real_escape_string($string)
    {
        $result = $this -> connection -> real_escape_string($string);   
        if($result) 
        {
            return $result;
        } 
        else
        {
            $this -> error = $this -> connection -> error;
            return false;
        }

    }

    #display error
    public function get_error() 
    {
        return $this -> error;
    }

    #closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> connection -> close();
    }
}
?>

or should I just change the host for good!??

below is the implementation of the database connection class. If I take this part out, the error won't appear anymore, but I do the same at other parts of the site as well and they won't cause any problem!

<!-- side-video-library -->
<div id="side-video-library" class="round-corner">

    <h4><a href="<?php echo HTTP_ROOT;?>videos"><span>ENER VIDEO LIBRARY</span></a></h4>

    <?php
    $sql = "
    SELECT *
    FROM root_pages

    WHERE root_pages.parent_id = '8'
    AND root_pages.pg_highlight = '1'
    AND root_pages.pg_hide != '1'
    ORDER BY rand() DESC
    LIMIT 1
    ";

    #instantiate the object of __database class
    $object_item = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    $item = $object_item -> fetch_assoc($sql);

    #instantiate the object of __database class
    $object_item_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    $total_item = $object_item_num -> num_rows($sql);
    //echo $total_item;
    ?>

    <?php
    if ($total_item > 0)
    {
        $sql = "
        SELECT *
        FROM root_tagged

        LEFT JOIN root_tags ON ( root_tags.tag_id = root_tagged.tag_id )

        WHERE root_tagged.pg_id = '".$item['pg_id']."'
        ";

        #instantiate the object of __database class
        $object_tagname = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $item_tagname = $object_tagname -> fetch_assoc($sql);

        #instantiate the object of __database class
        $object_tagname_num = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $total_tagname = $object_tagname_num -> num_rows($sql);
    ?>
    <p class="item-video">
        <object style="width: 183px; height: 151px;" width="183" height="151" data="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>" type="application/x-shockwave-flash">
            <param name="wmode" value="transparent" />
            <param name="src" value="http://www.youtube.com/v/<?php echo get_video_id($item['pg_content_1']) ;?>" />
        </object>
    </p>

    <h3><a href="<?php echo HTTP_ROOT.str_replace(' ', '-', 'videos').'/'.$item_tagname['tag_name'].'/'.str_replace(' ', '-', strtolower($item['pg_url']));?>"><?php if(strlen($item['pg_title']) > 20) echo substr($item['pg_title'], 0,20).'...'; else echo $item['pg_title'];?></a></h3>

    <p class="item-excerpt-video"><?php if(strlen($item['pg_content_2']) > 100) echo substr($item['pg_content_2'], 0,100).'...'; else echo $item['pg_content_2'];?></p>
    <a href="<?php echo HTTP_ROOT;?>videos" class="button-arrow"><span>More</span></a>
    <?php
    }
    ?>
</div>
<!-- side-video-library -->

Have I been implementing the class incorrectly??

thanks.

解决方案

Probably the problem is that you have only a handful connections allowed and when your class tries to get a new connection you have this error.

This is not a programming problem, just quantity of resources available. And any other script that uses this class are subject to have the error.

You have to configure more connections on mysql config file on the server. If you don't have this access, ask the support to do it or change for a hosting company with more connections allowed!

Other option is to implement a Singleton pattern on this class, so it reuses same pool of connections, and don't explode the limit.

这篇关于MYSQLi 错误:用户已经有多个“max_user_connections"活动连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆