Monday, March 28, 2011

friendlier error message for mysql 'max_user_connections'

I have a website that is MySQL/PHP. Occasionally, I cannot connect because of

Warning: mysql_connect() [function.mysql-connect]:
User foo has already more than 'max_user_connections' active connections in /home/foo/public_html/utilities.php

I use a discount web host service and can't really prevent it from happening occasionally. (At least I don't think I can.) How can I give the user a friendlier message than this crypic one?

From stackoverflow
  • Pear MDB2

    I would use Pear MD2B, better error message, good layer between your code and the database too.

    There are a lot more benefits as well. It is pretty easy to change your existing code as well.

  • If you turn off displaying errors (which you should probably do in production anyway), then you can then print your own error if the connection attempt fails.

    ini_set('display_errors', false);
    if (!$link = mysql_connect($host, $user, $pass)) {
        die('could not connect...');
    }
    

    If you can't change the ini setting, you can suppress the warning with @

    if (!$link = @mysql_connect($host, $user, $pass)) {
        die('could not connect...');
    }
    

    If you suppress the warning, you won't be able to see why the connection failed, which may or may not be what you want. However you could instead log the errors.

0 comments:

Post a Comment