title: What can cause DB PDO Connection failed: SQLSTATE[08004] [1040] Too many connections
body: <p>I've been stumped on this error for days now and I can't find a way to fix it. I've tried to implement</p>
  
  <ul>
  <li>Singleton database object</li>
  <li>Closing my connections after queries</li>
  <li>Trying a persistence connection</li>
  <li>Setting max connection to 250 (local)</li>
  <li>Setting max user connections 250 (on local)</li>
  </ul>
  
  <p>Has this occurred to anyone else? </p>
  
  <hr>
  
  <p>What's weird is the error message on my local server is <code>Connection failed: SQLSTATE[08004] [1040]</code> but the rows are still inserts and database queries work. </p>
  
  <p>On the production server, the error message becomes <code>Connection failed: SQLSTATE[42000] [1203] User db_admin already has more than 'max_user_connections' active connections</code> I know maximum connections is set to 15, but I have consulted support and they have said I have had no connections at the time of this error. </p>
  
  <p>After all my options, can someone check my code to make sure that there's no errors in it that can be creating these errors?</p>
  
  <pre><code>class MyPDO extends PDO{
  
      const DB_HOST='localhost';
      const DB_PORT='3306';
      const DB_NAME='db_name';
      const DB_USER='db_admin';
      const DB_PASS='SECRET';
  
      public function __construct(){
  
          parent::__construct('mysql:host='.MyPDO::DB_HOST.';port='.MyPDO::DB_PORT.';dbname='.MyPDO::DB_NAME,
                              MyPDO::DB_USER,MyPDO::DB_PASS);
          try{
                  $this-&gt;db = new MyPDO();
                  $this-&gt;db-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
              }catch(PDOException $e){
                  //always catching the connection errors
                  echo 'Connection failed: ' . $e-&gt;getMessage();
              }
      }
  
      public function query($query){ 
          $args = func_get_args();
          array_shift($args); 
          //check inputs
          var_dump($args);
          $reponse = parent::prepare($query);
          $reponse-&gt;execute($args);
          return $reponse;    
      }  
  }
  </code></pre>
  
  <p>Here is the insert query</p>
  
  <pre><code>function InsertUserToSql()
      {
          $ret = $this-&gt;db-&gt;query("INSERT INTO CD_USERS (email,username,password,fname,lname,dob,gender) VALUES 
          (?,?,?,?,?,?,?)",$this-&gt;Email,$this-&gt;Username,$this-&gt;Password,$this-&gt;Fname,
          $this-&gt;Lname,$this-&gt;Birthday,$this-&gt;Gender);
          if($ret){
              echo "insert success";
              return $this-&gt;db-&gt;lastInsertId();
          } 
          $this-&gt;db = null;
      }  
  </code></pre>