Ir para conteúdo
Fórum Script Brasil
  • 0

Erro no PDO MAX_USSER_CONNETIONS


gsinhorini

Pergunta

Olá, muito boa tarde!

Estou desenvolvendo uma plicação PHP com Composer, na maquina local está tudo funcionando mas quando eu coloco no servidor esta dando o seguinte erro.

Fatal error: Uncaught PDOException: SQLSTATE[42000] [1203] User meucarr2_admin already has more than 'max_user_connections' active connections in 

 

Essa é a minha class conexão. Se alguém puder me ajudar. muito obrigado.

<?php
namespace Api\Repositories;
use \PDO as PDO;

class Conexao
{
	protected $conn;
	protected $bind;
	
	function __construct()
	{
		//$this->connection();
	}

	function connection(){

        if($_SERVER["HTTP_HOST"]=="localhost"){
			$_username="root";
			$_password="";
			$_server="localhost";
			$_database="liderveiculos";

        }
        else{
        	//dados banco de producao
            // $_username="";
            // $_password="";
            // $_server="";
            // $_database="";

            $_username="";
            $_password="";
            $_server="";
            $_database="";
        }


        try {
            $_conn = new PDO('mysql:host='.$_server.';dbname='.$_database, $_username, $_password);
            // PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8";
            $_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $_conn->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND,"SET NAMES utf8");
            $_conn->setAttribute(PDO::ATTR_PERSISTENT,TRUE);
            //OUTROS MODOS
            /*
                PDO::ERRMODE_SILENT
                PDO::ERRMODE_WARNING
                PDO::ERRMODE_EXCEPTION
            */
            $this->conn=$_conn;
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
		
	}

	function select($_sql){
		unset($this->bind);

		$_query='SELECT '.$_sql["fields"].' FROM '.$_sql["tables"];

		if(isset($_sql["where"]) && count($_sql["where"])>0)
		{
			
			$_query.=$this->where($_sql["where"]);
            if(isset($_sql["econdition"]) && $_sql["econdition"]!="")
            {

                $_query.=' AND '.$_sql["econdition"];

            }
		}
		else
		{
			
			if(isset($_sql["econdition"]) && $_sql["econdition"]!="")
			{
				
				$_query.='  '.$_sql["econdition"];

			}	
		}



        if(isset($_sql["order"]) && $_sql["order"]!="")
		{
		
			$_query.=' ORDER BY '.$_sql["order"][0].' '.$_sql["order"][1];

		}
				
		//echo $_query;
        if(isset($this->bind))
		    $results= $this->executeQuery($_query,$this->bind);
        else
            $results= $this->executeQuery($_query);
	
		return $results;

	}

	function where($_where)
	{
		//echo '<pre>';
		//print_r($_where);
		//echo '</pre>';

		//unset($this->bind);
		$_queryWhere="";

		$_operators=array(	'=='=>'=',
							'!='=>'<>',
							'IN'=>'IN ( # )',
							'BETWEEN'=> '( #c BETWEEN #v1 AND #v2 )',
							'>'=>'>=');


		foreach ($_where as $_condition) {
			
			if(trim($_condition[0])=='OR')
			{
				$_increment=' OR ';
			}
			else
			{
				$_increment=' AND ';
			}

			$_findOperator=array_intersect($_condition, array_flip($_operators));
			
			//print_r($_findOperator);
			//echo count($_findOperator);
			if(count($_findOperator)>0)
			{
				foreach ($_findOperator as $_index => $_value) {
					$_op= $_operators[$_value];
					$_keyOperator=$_index;
					//echo $_value;
					//echo $_index;
				}

				//echo $_op;
				//echo $_keyOperator;

				$_keyCondition=array_search(array_search($_op, $_operators), $_condition);
				//print_r(array_search(array_search($_op, $_operators), $_condition));
				//	print_r($_keyCondition);
				//$_op=array_key_exists(key, search)
				//echo '--->'.strstr('#', $_op);


				if(!strstr('#', $_op))
				{
					//echo 2;

					switch ($_value) {
						case 'IN':
									$_queryWhere.=$_increment.' '.$_condition[$_keyCondition-1].' '.str_replace("#",  ' :'.$this->normalizeBind($_condition[$_keyCondition-1]), $_op);	
									$this->parameters($this->normalizeBind($_condition[($_keyCondition-1)]),$_condition[($_keyCondition+1)]);
							break;
						
						case 'BETWEEN':
									$_arrayReplace=array('#c'=>$_condition[$_keyCondition-1],
														 '#v1'=>':'.$this->normalizeBind($_condition[$_keyCondition-1].'_1'),
														 '#v2'=>':'.$this->normalizeBind($_condition[$_keyCondition-1].'_2'));
									
									$_queryWhere.=$_increment.strtr($_op, $_arrayReplace);
									$this->parameters($this->normalizeBind($_condition[$_keyCondition-1].'_1'),$_condition[$_keyCondition+1]);
									$this->parameters($this->normalizeBind($_condition[$_keyCondition-1].'_2'),$_condition[$_keyCondition+2]);

									//echo $_queryWhere.=' '.$_condition[$_keyCondition-1].' '.$_op.' :'.$this->normalizeBind($_condition[$_keyCondition+2]).$_increment;	
									//$this->parameters($_condition[($_keyCondition-1)],$_condition[($_keyCondition+1)]);
							break;
						default:
								//echo 'default';
								$_queryWhere.=$_increment.$_condition[$_keyCondition-1].' '.$_op.' :'.$this->normalizeBind($_condition[$_keyCondition-1]);	
								//echo $_condition[($_keyCondition-1)].'<>'.$_condition[($_keyCondition+1)];
								//echo $this->normalizeBind($_condition[($_keyCondition-1)]).'<>'.$_condition[($_keyCondition+1)];
								$this->parameters($this->normalizeBind($_condition[($_keyCondition-1)]),$_condition[($_keyCondition+1)]);
							break;
					}

					// $_queryWhere.=' '.$_condition[$_keyCondition-1].' '.$_op.' :'.$this->normalizeBind($_condition[$_keyCondition-1]).$_increment;	
					// $this->parameters($_condition[($_keyCondition-1)],$_condition[($_keyCondition+1)]);
				}
				else
				{
					// echo  ' '.$_condition[$_keyCondition-1].' '.$_op.' '.$_increment;
					$_queryWhere.=$_increment.str_replace('#', ' :'.$this->normalizeBind($_condition[$_keyCondition-1]).' ', ' '.$_condition[$_keyCondition-1].' '.$_op);						
					$this->parameters($this->normalizeBind($_condition[($_keyCondition-1)]),$_condition[($_keyCondition+1)]);	

				}
				
			}
			else
			{
				 $_queryWhere.=$_increment.' '.$_condition[0].' = :'.$this->normalizeBind($_condition[0]);
				$this->parameters($this->normalizeBind($_condition[0]),$_condition[1]);
			}

			
			
		}

		return ' WHERE '.substr($_queryWhere,4);

	}

	function parameters($_index,$_value)
	{
		//echo $_index.'<->'.$_value;
		$this->bind[':'.$_index]=utf8_decode($_value);
	}
	function normalizeBind($_string)
	{
		return str_replace(".", "_", $_string);
	}

	function executeQuery($_query,$_bind=null)
	{

 		/*echo '<pre>';
 		echo $_query;
 		echo '<br/>';
 		print_r($_bind);
 		echo '</pre>';*/

		try {
			$this->connection();
		
			//echo $_query;
			$_sql=$this->conn->prepare($_query);
			
			$this->conn->beginTransaction(); 

			$_result=$_sql->execute($_bind);			

		//var_dump($_sql);
		//exit();

			$_tipoQuery=strtoupper(substr(trim($_query),0,6));

			if($_tipoQuery=='SELECT' )
			{
				$_resultado=array();
                while($_row=$_sql->fetch(PDO::FETCH_ASSOC)) {
                    $_resultado[]=array_map('utf8_encode',$_row);
                    //$_resultado[]=$_row;
                }

                $this->conn->commit();
			   // var_dump($_resultado);
			    return $_resultado;
			}
			elseif($_tipoQuery=='INSERT'){
				//$_sql->commit();
				//oracle não possui
				$_ultimoId= $this->conn->lastInsertId();

				$this->conn->commit();
				return $_ultimoId;
				//return true;
			}
			elseif($_tipoQuery=='DELETE'){
				//$_sql->commit();
				//$_ultimoId= $this->conn->lastInsertId();

				$this->conn->commit();
				return true;

			}
			elseif($_tipoQuery=='UPDATE'){
				//$_sql->commit();
				//$_ultimoId= $this->conn->lastInsertId();
				
			
				$this->conn->commit();
				return true;
			
			}
			else{
				return $_result;
			}
			$this->conn->close();
			
		} catch(PDOException $e) {
            $this->conn->rollBack();

            echo 'ERROR: ' . $e->getMessage();
		    echo '<br/>';
		    echo 'Query:'.$_query;
		    echo '<br/>';
		    echo 'Bind:';
		    print_r($_bind);
		    echo '<pre>';
		    //print_r($e);
		    echo '</pre>';
		    $this->conn->close();
		}
		
	}

	function update($_sql)
	{
		unset($this->bind);
		$_query='UPDATE '.$_sql["tables"].' SET ';
		$_set="";
		$key="";
		foreach ($_sql["set"] as $key => $value) {
			//$this->parameters('1',1);
			$_set.=''.$key.'= :'.$key.'_ , ';
			//echo $this->normalizeBind($key.'_').'->'.$value;
			$this->parameters( $this->normalizeBind($key.'_'),  $value);	
		}

		$_query.=substr($_set,0,-2);

		if($_sql["where"] && count($_sql["where"])>0)
		{
			$_query.=$this->where($_sql["where"]);
		}
		else
		{
			$_query.=' WHERE '.$_sql["econdition"];
		}

		//$_query.=$_sql["econdition"];
		//echo $_query;
		//print_r($this->bind);

		return $this->executeQuery($_query,$this->bind);
	}


	function insert($_sql)
	{
		unset($this->bind);
		$_query='INSERT INTO '.$_sql["tables"].' ';

		$_into="";
		$_value="";

		foreach ($_sql["values"] as $key => $value) {
			//$this->parameters('1',1);
			$_into.=$key.' , ';
			$_value.=':'.$key.' , ';
			//echo $this->normalizeBind($key.'_').'->'.$value;
			$this->parameters( $this->normalizeBind($key),  $value);	
		}

		$_query.='('.substr($_into,0,-2).') VALUES ('.substr($_value,0,-2).')';

		return $this->executeQuery($_query,$this->bind);

	}

	function delete($_sql)
	{
		unset($this->bind);
		$_query='DELETE FROM '.$_sql["tables"].' ';

		if($_sql["where"] && count($_sql["where"])>0)
		{
			
			$_query.=$this->where($_sql["where"]);
		}
		else
		{
			
			if(isset($_sql["econdition"]) && $_sql["econdition"]!="")
			{
				
				$_query.=' WHERE '.$_sql["econdition"];

			}	
		}
		
		//return $_query;
		return $this->executeQuery($_query,$this->bind);


	}

	
}



?>

 

Link para o comentário
Compartilhar em outros sites

3 respostass a esta questão

Posts Recomendados

  • 0
Em 13/02/2019 em 17:19, gsinhorini disse:

Fatal error: Uncaught PDOException: SQLSTATE[42000] [1203] User meucarr2_admin already has more than 'max_user_connections' active connections in 

Mais conexões do que o parâmetro max_user_connections.

Provavelmente você terá que alterar isso no servidor ou alterar a classe para o padrão Sigleton

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,5k
×
×
  • Criar Novo...