Jump to content
xisto Community
snlildude87

Php Code To Detect When Mysql Db Is Down? a way to create a var if db is down

Recommended Posts

Recently, I recoded a part of my site so that it uses a MySQL database to store information instead of on a text file. Because my site receives a lot of traffic and it's on a shared hosting environment, the DB could potentially go down. Since I don't want to lose my visitors, I want to know if there is a way to tell when the MySQL DB is down. I know that I could add a die function along with the mysql_connect function:

mysql_connect("localhost", "username", "passwordl") or die("Something here.");

I want it to do more than just print a line stating the problem--I want it to store a variable. Is there a way to do such thing?

Thanks!

Share this post


Link to post
Share on other sites

id say this:

 

db.php

$sql_conn = mysql_connect("host","user","pass");if(!$sql_conn){$db_status = 0;} else {$db_status = 1;}

Checks if a mysql connection was established, stored as a "0" or "1"

 

Next step would be further developing that variable in the page where you want to show the status:

 

Any other page

include("db.php");#if db is onlineif($db_status=="1"){$db_status = "<img src='images/online.gif' border='0'>";}#else db is downelse {$db_status = "<img src='images/offline.gif' border='0'>";}print "Database Status: $db_status";

configure it to your own liking, once db.php is included you can make the $db_status variable anything you want :D

 

Enjoy!

Share this post


Link to post
Share on other sites

when you perform mysql querys and other things that call for the database, you seem to be always using die()?

not needed, just store the query or whatever it is your using in a variable and check the query with an if...else statement.

 

Instead of using:

mysql_query("SELECT * FROM table") or die(mysql_error());

you should do:

$qry = mysql_query("SELECT * FROM table");

if(!$qry){$db_status = 0;} else {$db_status = 1;}

 

Continued from where i left off :D

 

Hope this helps.

Share this post


Link to post
Share on other sites

If you used this:

 

$qry = mysql_query("SELECT * FROM table");

 

if(!$qry){$db_status = 0;} else {$db_status = 1;}[/code]

 

then the part in else {} will execute and everything below that.

 

Unless, you put some exit code in that else statement, of course.

216145[/snapback]


Gotcha. So the rest will continue to execute.

 

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.