在远程服务器上运行我的代码时出错:用户 root 已超出 'max_user_connections'

error when runing my code in remote server: User root has exceeded the 'max_user_connections'

[我使用 hostinger 作为主机] 我有一个显示查询结果的网页,它在本地工作,但是当我将它上传到网络时,我收到一条错误消息:

Ha habido un error en la linea: 6 Error: SQLSTATE[42000] [1226] User 'u749120824_root' has exceeded the 'max_user_connections' resource (current value: 3) Fatal error: Call to a member function prepare() on a non-object in /home/u749120824/public_html/pags/ver_usuarios.php on line 18

这是一些屏幕截图 本地托管

在线托管 我的代码:

class ComprobarUsuario extends Conexion{
        public function ComprobarUsuario(){
            parent::__construct();
        }
        public function Comprobar($nombre){
            $sqlCU="SELECT * FROM admins WHERE NOMBRE=:nombre_usuario";
            $resultCU=$this->con->prepare($sqlCU);
            $resultCU->execute(array(":nombre_usuario"=>$nombre));
            $NresultadosCU=$resultCU->fetch(PDO::FETCH_ASSOC);
            if($NresultadosCU!=0){
                return "admin";
            }
        }
    }

连接class:

class Conexion{
        protected $con;
        public function Conexion(){
            try{
                $this->con=new PDO("mysql:host=localhost;dbname=marisol","root","");
                $this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                $this->con->exec("SET CHARACTER SET utf8");
                return $this->con;
            }
            catch(PDOException $e){
                echo "Ha habido un error en la linea: " . $e->getLine() . " Error: " . $e->getMessage();
            }
        }
    }

问题是您的代码为 ComprobarUsuario 的每个新实例创建了一个新连接。如果您对其他查询使用类似的方法,您的应用程序将很快超出限制。解决方案是为所有子类.

创建一个共享连接
class Conexion{
        protected static $con;
        public function Conexion(){
            try{
                if (!self::$con) {
                    self::$con=new PDO("mysql:host=localhost;dbname=marisol","root","");
                    self::$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
                    self::$con->exec("SET CHARACTER SET utf8");
                    return self::$con;
                }
            } catch(PDOException $e){
                echo "Ha habido un error en la linea: " . $e->getLine() . " Error: " . $e->getMessage();
            }
        }
    }