Create a '500 internal server error' code for mysql too many connections error

Hi Guys,

I didnt know wether to post this in here, apache or mysql category because its a bit of a mixture. However I want my server to return a 500 error code when mysql has “too many connections”.

Currently, if my server gets overloaded for whatever reason all the pages will get a 200 OK response if all the mysql connections are used up. And if Google are crawling my site at the same time, its not good news!

Any ideas?

Cheers,

Mike

you can send any HTTP header using header() function.
so, after you check connection result, 500 header can be sent.

Hi, thanks for the reply.

Do you know what the script would be for that?

cheers,

mike


if(false === ($connection = mysqli_connect(/**/))){
    if(1203 === mysqli_connect_errno()){
        header('HTTP/1.1 500 Internal Server Error');
        exit;
    }
}

:wink:

Hi Anthony,

thanks a lot for the reply, however that didnt seem to work?

I got the following error on the frontend;

Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user ‘nobody’@‘localhost’ (using password: NO) in /XXXX/XXXXX/public_html/includes/XXXX.inc.php on line 19

Any ideas?

Cheers,

Mike

you may be took this code too literally
this was an example. not to copy and paste but to understand and implement.
mysqli_connect() usually require not empty comment but some parameters, which obviously unknown to Anthony…

also you must turn displaying errors off on your public server,
by using
ini_set(“display_errors”, 0);
ini_set(“log_errors”, 1);

Hi Shrapnel,

Ah ok! Thanks.

But which parameters need to be in there? the username, db, and password? What format would that be in if so?

Cheers,

Mike

you have this function in your code already, with all it’s parameters

Shrapnel, what do you mean?

I am a little confused.

I have this function in my site at the moment;

if ( $productId == "" )	{
		header("HTTP/1.1 404 Not Found");
		include "404.php";
		exit;
	}

This produces a 404 if there is no product id found. What can I change this too so that it does the 404 thing, but also produces a 500 if mysql cant connect.?

cheers,

mike


<?php
if(false === ($connection = mysqli_connect('host', 'username', 'password', 'database', 3306))){
    if(1203 === mysqli_connect_errno()){
        header('HTTP/1.1 500 Internal Server Error');
        exit;
    }else{
        #other connection error
    }
}
?>

Change the parameters passed to mysqli_connect with valid credentials which you are no doubt already using to obtain the product details.

:slight_smile:

Excellent, thanks Anthony! :slight_smile:

These are the headers I use and what I use them for.

HTTP/1.0 200 Ok
This is both Apache and PHP’s default header.

HTTP/1.0 207 Updated
This response code is used exclusively to reply to the Javascript side of my framework. The javascript code infers that PHP has completed its share of the action when it sees this code. The browser does not cache any response with this header (which is why it is used instead of simply 200 ok.

HTTP/1.0 209 Partial Content
This is used when PHP is sending an XML or JSON file with an HTML snippet that javascript needs to append into the page. Again, this code isn’t normally cachable.

HTTP/1.0 320 Reload
Another header sent only in response to an Ajax request, when my Javascript code reads this header it finishes whatever it’s doing and then reloads the entire page including itself. This header is not part of the standard so browsers will treat it as if it was code 300, which is the redirect class of codes.

HTTP/1.0 400 Bad Request
The client side code sent something the server didn’t expect. I raise this error when forms don’t validate and sending the error page back. Again, like most error codes caching is avoided.

HTTP/1.0 401 Unauthorized
User has no permission to perform the action requested.

HTTP/1.0 403 Forbidden
As above, but generally used in response to file access attempts.

HTTP/1.0 404 Not Found
Anyone not use this yet :stuck_out_tongue:

HTTP/1.0 500 Internal Server Error
I send this header with any PHP exception that escapes since I consider the script part of the server.

HTTP/1.0 520 Programmer Debug Output
My debug functions send this header. Again, not part of the standard, will be treated as a vanilla 500.