mysql'max_user_connections'的友好错误消息

时间:2022-10-02 19:12:34

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

我有一个MySQL / PHP网站。偶尔,我无法连接因为

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?

我使用折扣网络托管服务,并不能真正防止它偶尔发生。 (至少我认为我不能。)我怎样才能给用户一个比这个更加隐秘的消息呢?

2 个解决方案

#1


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 @

如果您无法更改ini设置,则可以使用@禁止警告

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.

如果您取消警告,您将无法看到连接失败的原因,这可能是您想要的,也可能不是。但是,您可以改为记录错误。

#2


Pear MDB2

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

我会使用Pear MD2B,更好的错误信息,代码和数据库之间的良好层。

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

还有很多好处。更改现有代码也非常容易。

#1


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 @

如果您无法更改ini设置,则可以使用@禁止警告

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.

如果您取消警告,您将无法看到连接失败的原因,这可能是您想要的,也可能不是。但是,您可以改为记录错误。

#2


Pear MDB2

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

我会使用Pear MD2B,更好的错误信息,代码和数据库之间的良好层。

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

还有很多好处。更改现有代码也非常容易。