PHP Detect Mysql Too many connections

Detect "Too many connections" error and show alternate web page

<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
if (mysql_errno() == 1203) {
// 1203 == ER_TOO_MANY_USER_CONNECTIONS (mysqld_error.h)
header("Location: http://your.site.com/alternate_page.php");
  exit;
}
?>

 

<?
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
?>

Leave a comment