DTP


 
Lively discussions on the graphic arts and publishing — in print or on the web


Go Back   Desktop Publishing Forum > General Discussions > Web Site Building & Maintenance

Reply
 
Thread Tools Display Modes
Old 12-09-2009, 01:30 AM   #1
john_b
Member
 
Join Date: May 2006
Location: Scottish Borders, UK
Posts: 208
Default PHP connection to MySQL database

I noted recently that a website that I run was down in that none of the PHP scripts could connect to the MySQL database. The site is hosted remotely, I called them as soon as I found out and the problem was solved after a reboot of the MySQL server. There was no apparent reason for the failure...

...so it set me wondering. Had I been away, say for a few days, then the situation could have been more serious and I wouldn't have known anything about it for some considerable time.

I was thinking that instead of what seems to be common practice in PHP of saying connect to the database or display a rather unhelpful error message along the lines of

or die ("sorry, could not connect");

perhaps it would be better to also send an email alert to the administrator saying that there may be a problem.

That seems feasible, but is it a) recommended practice and b) a wise plan, given that it may well swamp your mailbox on a busy site?

On the one hand, if such a problem does arise then you'd like to know about it asap, but on the other hand, might there be an equally serious downside?

John
john_b is offline   Reply With Quote
Old 12-09-2009, 05:39 AM   #2
Steve Rindsberg
Staff
 
Join Date: Nov 2004
Posts: 6,713
Default

I don't know PHP but I'd expect that you could check an Email_sent boolean variable before sending the email. Pseudocodially:

If Email_sent then
' already sent the email; nothing to do
Else
SendTheEmail
Email_sent = True
End if

You'd then run another script or visit a page that runs it for you once you'd sorted the problem. This other script would set Email_sent back to false.

   
__________________
Steve Rindsberg
====================
www.pptfaq.com
www.pptools.com
and stuff
Steve Rindsberg is offline   Reply With Quote
Old 12-09-2009, 05:45 AM   #3
donmcc
Member
 
Join Date: Feb 2005
Location: Sarnia, Canada
Posts: 1,122
Default

It's been a while since I was coding php, but could you get a routine to run on the fail, before the die, that would email the admin one time, and then ignore itself until the problem is fixed?

If a site wasn't huge, your idea of asking for a notification might work, but I would hate to have 3000 emails come in from a huge site after a weekend (your B scenario).

Don
donmcc is offline   Reply With Quote
Old 12-09-2009, 08:49 AM   #4
john_b
Member
 
Join Date: May 2006
Location: Scottish Borders, UK
Posts: 208
Default

Quote:
Originally Posted by Steve Rindsberg View Post
I don't know PHP but I'd expect that you could check an Email_sent boolean variable before sending the email.
Thanks for that and I think I'd got as far as deciding that, yes, I want an email alert but just the one email will do it, thanks very much!

Setting a flag, as you say, is the way to go. So where to store it? I thought maybe I'd just write it into the database somewhere...

...but a few moments of more mature reflection reveals the folly of that approach! Doh!!!

However, I guess I can just write it to a small text file - that should do it.

On a wider issue, I'm no PHP maven, but it does seem that this kind of error handling doesn't feature too prominently in the standard - but possibly limited - texts and tutorials that I've come across. I just wondered if there's an approach that finds general favour among those who know about these things.

John
john_b is offline   Reply With Quote
Old 12-09-2009, 08:50 AM   #5
john_b
Member
 
Join Date: May 2006
Location: Scottish Borders, UK
Posts: 208
Default

Quote:
Originally Posted by donmcc View Post
It's been a while since I was coding php, but could you get a routine to run on the fail, before the die, that would email the admin one time, and then ignore itself until the problem is fixed?
Thanks for that and I do like the notion of a routine that ignores itself...;-)

Just joking, and I think you're right - just not quite sure if there's an elegant way to achieve it. Probably fair to say, though, that my PHP coding and the word 'elegant' don't sit easily together.

I think it's possible to do away with the 'die' part and replace it with a function that does whatever's necessary and then 'exit's any further processing.

So unless an 'elegant' option emerges I think I'll be going for the somewhat clunky approach as in my reply to Steve.

John
john_b is offline   Reply With Quote
Old 12-09-2009, 09:07 AM   #6
Steve Rindsberg
Staff
 
Join Date: Nov 2004
Posts: 6,713
Default

" ... among those who know about these things."

There's my exit line.

   
__________________
Steve Rindsberg
====================
www.pptfaq.com
www.pptools.com
and stuff
Steve Rindsberg is offline   Reply With Quote
Old 12-09-2009, 10:51 AM   #7
annc
Sysop
 
annc's Avatar
 
Join Date: Oct 2004
Location: Subtropical Queensland, Australia, between the mountains and the Coral Sea
Posts: 4,434
Default

Quote:
Originally Posted by john_b View Post
perhaps it would be better to also send an email alert to the administrator saying that there may be a problem.

That seems feasible, but is it a) recommended practice and b) a wise plan, given that it may well swamp your mailbox on a busy site?
This is normal practice for vBulletin forums (the one used here). I use a specific account for the administrator, so that any notifications don't get lost among other more general e-mails. It's been very useful on the few occasions when there's been a problem on the server at the hosting company I use.

   
__________________
annc is offline   Reply With Quote
Old 12-10-2009, 12:52 AM   #8
john_b
Member
 
Join Date: May 2006
Location: Scottish Borders, UK
Posts: 208
Default

I think what highlighted the issue was the fact that the website itself was accessible but the MySQL server was down - I don't think I've come across that combination before.

Clearly if the web server is down then you're up a gum tree, but if just the database is out then you can at least send an alert to say so.

Your idea of a dedicated email account is a good one and I think certainly I'll go for that!

John
john_b is offline   Reply With Quote
Old 12-10-2009, 07:34 AM   #9
ktinkel
Founding Sysop
 
ktinkel's Avatar
 
Join Date: Oct 2004
Location: In Connecticut, on the Housatonic River near its mouth at Long Island Sound.
Posts: 11,189
Default

Quote:
Originally Posted by john_b View Post
I think what highlighted the issue was the fact that the website itself was accessible but the MySQL server was down - I don't think I've come across that combination before.

Clearly if the web server is down then you're up a gum tree, but if just the database is out then you can at least send an alert to say so.

Your idea of a dedicated email account is a good one and I think certainly I'll go for that!
Some hosting services deliberately place database and e-mail (and sometimes control panels) on different servers — Cartika, for one.

   
__________________
[SIZE=2][COLOR=LemonChiffon]::[/COLOR][/SIZE]
[SIGPIC][/SIGPIC]
ktinkel is offline   Reply With Quote
Old 12-10-2009, 09:48 AM   #10
john_b
Member
 
Join Date: May 2006
Location: Scottish Borders, UK
Posts: 208
Default

Yes I appreciate that different servers may be involved though I'm certainly no guru in that area. I think that what struck me about this incident was that I was assuming that the hosting company would be monitoring all these services, but now I'm not so sure...

I'd have to say that they've been pretty good since I moved to them a couple of years ago and I've not had too many problems. There have been odd occasions when email has been down or the website has been unavailable but in my experience they've been back up and running fairly quickly.

So I have assumed that if there is a problem then they will be alerted and fix it toot sweet.

But now I don't know how long it would have taken them to spot that my MySQL service was down and, worryingly, whether they would have spotted it. If I hadn't phoned them, what would have happened?

In any event, if it's possible to alert me of any similar problem in the future then that would seem to be the way to go...;-)

John
john_b is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
DSL & WinXP connection timeout problem terrie Software 41 04-25-2008 03:18 PM
Wireless as only connection Paul General Publishing Topics 9 05-30-2006 11:00 AM


All times are GMT -8. The time now is 07:46 AM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Contents copyright 2004–2014 Desktop Publishing Forum and its members.