Page 1 of 1

Database connectivity - what PHP debug code could I use?

Posted: Wed May 14, 2014 3:15 am
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.

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

Posted: Wed May 14, 2014 6:11 am
by Celauran

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

Posted: Wed May 14, 2014 6:51 am
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);
}

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

Posted: Wed May 14, 2014 8:30 am
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()

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

Posted: Wed May 14, 2014 10:33 am
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?

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

Posted: Wed May 14, 2014 4:25 pm
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.

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

Posted: Wed May 14, 2014 4:32 pm
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.

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

Posted: Wed May 14, 2014 6:01 pm
by Christopher
Database logs (and web server logs) are probably the place to check. Databases also often have live stats.

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

Posted: Thu May 22, 2014 3:55 am
by ryancody
The code is to connect $sqlconn = mysql_connect('localhost','root', '');