title: Mysql says user max connections exceeded
body: <p>I have a CMS that is failing when more than one person is logged in. I think the problem may be that I'm not closing databases properly. Here is the basic setup:</p>
  
  <p>I have a master class called Mysql. The __construct() method looks something like this:</p>
  
  <pre><code>class Mysql {
  
      protected $conn;
  
      function __construct() {
  
          if(!$this-&gt;conn) $this-&gt;conn = New mysqli(SITE_HOSTNAME, SITE_USERNAME, SITE_PASSWORD, SITE_DATABASE);
  
  }
  </code></pre>
  
  <p>All of my classes are descendants of this class, and so whenever they are instantiated, they get their own connection.</p>
  
  <p>I'm using prepared statements to protect against sql injection. A sample method might look like this:</p>
  
  <pre><code>function foo($blog = 0) {
  
      $query = "SELECT catid, catname
                FROM category
                WHERE blog = ?
                ORDER BY catname ASC";
  
      $result = array();
      if($stmt = $this-&gt;conn-&gt;prepare($query)) {
          $stmt-&gt;bind_param('i', $blog);
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($catid, $catname);
          while($stmt-&gt;fetch()) $result[$catid] = stripslashes($catname);
          $stmt-&gt;close();
          return $result;
      }
  
  }
  </code></pre>
  
  <p>The Mysql __destruct() method looks like this:</p>
  
  <pre><code>function __destruct() {
      if($this-&gt;conn) $this-&gt;conn-&gt;close();
  }
  </code></pre>
  
  <p>Is my problem that I am not closing my connections properly, or that I do not have enough active connections?</p>
  
  <p>My provider says that I have 30 simultaneous connections available. Not enough?</p>
  
  <p>Thanks for your help.</p>