Forum Moderators: coopster

Message Too Old, No Replies

Unable to Connect

failsafe for connection failure

         

grandpa

10:22 am on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



On a new site of mine I've seen a connection problem a few times over the past several days. Each page on the site is a php page which includes code for tracking. Each page then requires a connection to the database, thus

<?php
$link = mysql_connect("SERVER", "USER", "PWD") or die("Could not connect : " . mysql_error());
?>

True to form, after a 60 second timeout I get the error displayed on the screen. Well, rather than display that error I'd like to redirect to a pure html document. So, I thought that the solution might be something like the following example, where I attempt to redirect the page to an html document.

<?php
$link = mysql_connect("SERVER", "USER", "PWD");
if (!link) {
header ("Location: http://example.com/index.html");
}
?>

Of course, by time I figured this out and got it uploaded, the server connection was restored. However, it seems like this will work. Notice that the redirect does not go to www, but to the root. There is a series of plain html documents there (not indexed) that will at least allow the visitor to see something. There are also .htaccess issues to work out, assuming everything else works the way I think it will. Any thoughts?

Also, I've been looking around for more documentation on mysql_connect and how to capture other errors, such as invalid user or password. It seems like it would be possible, but I haven't found anything yet.

roldar

10:26 am on Aug 22, 2005 (gmt 0)

10+ Year Member



<?php
$link = mysql_connect("SERVER", "USER", "PWD");
if (!link) {
header ("Location: http://example.com/index.html");
}
?>
----------

Don't forget the $ at: if(!$link) {

If you want to find out what error is occurring with a failed mysql_connect you could do this:

$link = mysql_connect("SERVER", "USER", "PWD") or die("Error :".mysql_error());

I'm not sure what it does when there's a timeout though.. maybe nothing.

grandpa

3:28 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't forget the $

Urk! Ok, I'll fix that right away ;)

But I'm wondering how one can capture an error for an invalid logon also. It shouldn't really ever happen I suppose, but it could.?..? The problem with my example is that $link will return true for an invalid logon attempt.

jatar_k

4:08 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



easy enough

use the error number as well as the mysql_error for the message
[php.net...]

you can use their messages or just get the error number and use your own user friendly message.

you'll need this too
[dev.mysql.com...]

grandpa

8:50 am on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks. That put me be back on the right track. This began as a timeout issue. Now I think I can still display a page irregardless of mysql connection problems. If a page needs products from a table it will be crippled without a connection, but at least displayed with my friendlier message.

This should also prevent SE indexing of pages displaying an error notice or warning.

jatar_k

2:22 pm on Aug 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could always have a static page you could include if you can't connect to the db or you could have a little csv file that you could use as your default set of products.