MYSQLi error: User already has more than 'max_user_connections' active connections
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.
Database Error: [mysqli.mysqli]: User already has more than 'max_user_connections' active connections
this should fix your problem, but i didn't test it. the difference: if getConnection()
is called the first time, a connection is made and stored. the rest of the time, the already established connection is used.
i removed the action in closeConnection, because that would make the first change useless. altough it would be nicer to remove the closeConnection call from execSQL.
normally (as far as i know), the database connection automatically closes when the script terminates (as long as mysqli doesn't support persistance). but it would be better to call (a working) closeConnection manually after all the database-stuff is finished.
<?php
class Model {
private $created;
private $modified;
private static $db = false;
static function getConnection()
{
/*
Connect to the database and return a
connection or null on failure
*/
if (self::$db === false) {
self::$db = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);
}
if(!self::$db) {
echo mysql_error();
throw new Exception('Could not connect to database', EX_NO_DATABASE_CONNECTION);
}
return self::$db;
}
static function closeConnection()
{
// self::$db->close();
}
// moar functions (...)
}
?>
and ... i'd recommend using an existing database access abstraction layer to avoid such problems in the future.
User co_p already has more than 'max_user_connections' active connections
mysql_close;
should be generating an error.
This command should be
mysql_close();
Add error reporting to the
top of your file(s) while testing right after your opening PHP tag for example
<?php error_reporting(E_ALL); ini_set('display_errors', 1);
to see if it yields anything.But I have to ask, why are you connecting to the database and then closing the connection straight after?
After more code added.
As you are actually trying to access the database, you dont want to do a mysql_close();
where you have coded it.
But I have to add:
Every time you use the
mysql_
database extension in new code
this happens
it is deprecated and has been for years and is gone for ever in PHP7.
If you are just learning PHP, spend your energies learning thePDO
ormysqli
database extensions and prepared statements.
Start here
User already has more than 'max_user_connections' active connections
Do not open and close connection on each file or worse on each query. Make a file similar to connection.php
where you put the mysql_connect statement and use require_once
where you want to use it.
do not use mysql_close
since the connection gets closed automatically each time the script finishes.
Related Topics
Are There Limits for Session Variables
How to Use Prepared Statements in Codeigniter
How to Merge Transparent Png with Image Using PHP
The Difference Between Unset and = Null
Include, Include_Once, Require or Require_Once
PHP Function to Delete All Between Certain Character(S) in String
How to Sort Rows of HTML Table That Are Called from MySQL
What Does Filter_Sanitize_String Do
How to Convert Soap Response to PHP Array
Scandir() to Sort by Date Modified
How to Check If an Integer Is Within a Range
Uncaught Reflectionexception: Class Log Does Not Exist Laravel 5.2
How to Load PHP File into Div by Jquery
PHP Send Mail to Multiple Email Addresses