Database connectivity - what PHP debug code could I use?
Moderator: General Moderators
-
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?
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.
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.
All the best from the United Kingdom.
-
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?
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.
All the best from the United Kingdom.
Re: Database connectivity - what PHP debug code could I use?
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?
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);Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- 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?
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?
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.
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.
All the best from the United Kingdom.
- 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?
Database logs (and web server logs) are probably the place to check. Databases also often have live stats.
(#10850)
Re: Database connectivity - what PHP debug code could I use?
The code is to connect $sqlconn = mysql_connect('localhost','root', '');