Re: when mysql is down

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Oct 17, 2008 at 11:52 AM, Rick Pasotto <rick@xxxxxxxx> wrote:
> Today my hosting company took down the mysql server for about 1/2 hour.
> As a result some php errors displayed.
>
> All of my pages have a random quote from a mysql table. If it's not
> available it's really not a big deal. However, some of the pages depend
> entirely on data from the database.
>
> What's the best way to handle this? If the mysql is required should I
> redirect to the front page (which doesn't need mysql except for the
> quote) or show a blank (or error message) content area (navigation would
> still be available as it's the same on all pages)?
>
> The quote is from an include file. What's the best way to output nothing
> if the mysql connection fails?
>
> I realize these are probably elementary questions but any advice would
> be appreciated.
>
> --
> "... the state ... is not armed with superior honesty, but
>  with superior physical strength." -- Henry David Thoreau
>    Rick Pasotto    rick@xxxxxxxx    http://www.niof.net
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Depends on how you're fetching the data.

For instance in my scripts if I'm showing a list of records I'll do it
like this:

$items = $itemGateway->fetch();
if ($items->isSuccess() !== true OR $items->count() === 0) {
    echo "<strong>No items exist</strong";
    return;
}

while ($item = $items->fetchAssoc()):
    // render content
endwhile;

This check ensures that if something goes wrong, I'm still covered.
Even if I can't connect it will just say no records exist.  Of course
there are other ways of handling this too because internally if I
can't connect it will throw an exception, so I could just not catch
the exception and it'd bubble up showing the user an error page.

I can do this because my ItemGateway will call my db query method.
This then triggers the connect if it hasn't established one already.
So depending on your set up you might have to go upstream from your
data fetching with checks.

Perhaps something like:
// at top of your script/db include
$con = mysql_connect()
if (!is_resource($con)) {
    set 500 error header
    include error page
}

or

// inside your random_quote.php
$sql = "SELECT RANDOM ENTRY";
$res = mysql_query($sql, $con);
if (!is_resource($res)) {
   // do nothing if random quote result doesn't exist
   return;
}

So decide for yourself if the database not connecting is a fatal error
or not.  If it is, then show an error page with a error header.
Otherwise just show a note saying oops.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux