0

I have this database class

 function MySQLDB(){
  /* Make connection to database */
  $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
  mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
  /**
   * Only query database to find out number of members
   * when getNumMembers() is called for the first time,
   * until then, default value set.
   */
  $this->num_members = -1;

  if(TRACK_VISITORS){
     /* Calculate number of users at site */
     $this->calcNumActiveUsers();

     /* Calculate number of guests at site */
     $this->calcNumActiveGuests();
  }


}

and I am getting max_user_connections error with only 12 users logged in. I saw this script that shows to toggle between user to connect if user exceeds quota.

 ini_set('display_errors', '0');
$link = mysql_connect("localhost", "user", "pass");
if (mysql_errno() == 1040 OR mysql_errno() == 1203) {
    define("DB_HOST", "some_ip:3306");//remote
    define("DB_NAME", "db");  //database_name
    define("DB_USER", "user"); //database user name
    define("DB_PASSWORD","pass");  //database (user) password
}
else
{
    define("DB_HOST", "localhost");
    define("DB_NAME", "db_name2");  //database_name
    define("DB_USER", "user"); //database user name
    define("DB_PASSWORD","pass");  //database (user) password
}
ini_set('display_errors', '1');
//by feha at www.vision.to

Can someone point me in the right direction of how to put this feature in my class. Thanks in advance.

UPDATE Will this work and reset $this->connection to a secondary user if there is a error?

function MySQLDB(){
  /* Make connection to database */
  $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
  if (mysql_errno() == 1040 OR mysql_errno() == 1203) {
  $this->connection = mysql_connect(DB_SERVER, DB_SEC_USER, DB_SEC_PASS) or die(mysql_error());    
  }
  mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
  /**
   * Only query database to find out number of members
   * when getNumMembers() is called for the first time,
   * until then, default value set.
   */
  $this->num_members = -1;

  if(TRACK_VISITORS){
     /* Calculate number of users at site */
     $this->calcNumActiveUsers();

     /* Calculate number of guests at site */
     $this->calcNumActiveGuests();
  }

}

Sean Bray
  • 71
  • 1
  • 5

0 Answers0