Database connectivity - what PHP debug code could I use?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Database connectivity - what PHP debug code could I use?

Post by simonmlewis »

I am getting the occasional message now that our database hasn't worked.
When I check the page it comes from, it's fine.
It tries to make the DB connection. If it cannot, it then emails me. It also emails me the page on which it failed.

I'm told I should add debugging code to the page. Apart from scripts within PHP that show errors on the page itself with display_errors turned on locally, I don't know how to spot what is causing it.

There is one opening db conn script at the top of the template, and one closing at the bottom. No include files have any DB Conn scripts. So I don't know how it can be "down".

Considering how many tens of thousands of hits a day the site gets, 3-4 emails this morning isn't bad. But be good to find the cause.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Database connectivity - what PHP debug code could I use?

Post by Celauran »

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Database connectivity - what PHP debug code could I use?

Post by simonmlewis »

Can I just pop that into the email I have sent to me, in the PHP Script and that would give me a log of the issue?

Code: Select all

if (!defined('DBUSER')) {
define('DBHOST', 'localhost');
define('DBUSER', 'root');
define('DBPASS', '');
define('DBNAME', 'site');
}

$sqlconn = mysql_connect(DBHOST, DBUSER, DBPASS);
if ($sqlconn) {
        mysql_select_db(DBNAME);
}

try {
    $pdo = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
} catch (Exception $e) {
$error = error_log();
        $to = "email";
        $subject =  "SiteDB Down";
        $headers = "From: info@site.co.uk";
        $body = "Database down.
$error
";
        mail ($to, $subject, $body, $headers);
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Database connectivity - what PHP debug code could I use?

Post by Celauran »

No. That's a function to write errors to your error log. If you're just going to mail the error to yourself, use the exception you're catching.

Code: Select all

$body = "DB Error: " . $e->getMessages()
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Database connectivity - what PHP debug code could I use?

Post by simonmlewis »

Code: Select all

        $to = "simon@domain";
        $subject =  "Site DB Down";
        $headers = "From: info@site.co.uk";
        $body = "DB Error: " . $e->getMessages();

        mail ($to, $subject, $body, $headers);
Like this?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Database connectivity - what PHP debug code could I use?

Post by Christopher »

It may not be in the code. Perhaps the connection is failing be cause there are no more connection available/allowed to the database for some reason.
(#10850)
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Database connectivity - what PHP debug code could I use?

Post by simonmlewis »

It's happening at times of day when site is least used. It gets hammered in the early evening.
Is there a way to see how many conns are open at any one time? I could perhaps add that tomthe email, so I can see that too.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Database connectivity - what PHP debug code could I use?

Post by Christopher »

Database logs (and web server logs) are probably the place to check. Databases also often have live stats.
(#10850)
ryancody
Forum Newbie
Posts: 14
Joined: Wed May 21, 2014 1:48 am

Re: Database connectivity - what PHP debug code could I use?

Post by ryancody »

The code is to connect $sqlconn = mysql_connect('localhost','root', '');
Post Reply