Page 1 of 1
timeout problem, cannot connect to db
Posted: Tue May 19, 2009 11:35 am
by eatspinach
Hi
I have a problem with some of my php pages.
The problem is when i execute a page like this,
printHeader();
printContent();
printFooter();
Sometimes printContent(); will fail because it cannot connect to the database, this means that printFooter(); will never get executed and i am just left with a half a page and no way to fix this.
Is their someway to make sure everything on a page gets executed even if the items before it fail?
Thanks
Re: timeout problem, cannot connect to db
Posted: Tue May 19, 2009 3:02 pm
by Darhazer
Probably you have die() in the printContent() ?
Use exceptions and try / catch blocks.
And also best is to separate presentation from the code, so first all the code is executed, and than you output what you have to output
Re: timeout problem, cannot connect to db
Posted: Wed May 20, 2009 5:42 am
by eatspinach
Thanks for your reply, I am still a bit confused.
Here is a more detailed view of my code,
Code: Select all
<?php
if (count($warnings)) {
print_warnings($warnings);
}
else {
print_summary_details($from, $to, $fhr, $thr, $frange, $trange, $comment, $offered, $abandoned, $showall, $surveyid);
print_average_question_score_details($from, $to, $fhr, $thr, $frange, $trange, $comment, $offered, $abandoned, $showall, $surveyid);
print_call_log($from, $to, $fhr, $thr, $frange, $trange, $comment, $abandoned, $showall, $surveyid);
}
?>
<div id="sb">
<?php
print_call_log_form($fdate, $fmonth, $fyear, $tdate, $tmonth, $tyear);
?>
</div> <!-- #sb -->
The problem is that sometimes the function "print_call_log()" brings back "error cannot connect to the database". This means that print_call_log_form(); never gets executed.
I tried putting a try catch around "print_call_log()" but this didn't seem to make any difference.
Here is what I tried perhaps i did it wrong,
Code: Select all
try
{
print_call_log($from, $to, $fhr, $thr, $frange, $trange, $comment, $abandoned, $showall, $surveyid);
}
//catch exception
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
Re: timeout problem, cannot connect to db
Posted: Wed May 20, 2009 3:39 pm
by Darhazer
Probably in the print_call_log() or in a function, called by it, there is a die() or exit()
for example:
Code: Select all
mysql_connect(...) or die("error cannot connect to the database")'
Replace the die() with:
Code: Select all
throw new Exception('Cannot connect to database');
So you can catch the exception and continue the execution of the script.
Re: timeout problem, cannot connect to db
Posted: Thu May 21, 2009 5:16 am
by eatspinach
I did find this line that could be causing the problem,
Code: Select all
stream_wrapper_register("xlsfile", "xlsStream")
or die("Failed to register protocol: xlsfile");
I am unable to replace the die function with a throw function,
I tried this
Code: Select all
stream_wrapper_register("xlsfile", "xlsStream")
or throw new Exception('Failed to register protocol: xlsfile');
But that didn't work, I just got this error
Parse error: syntax error, unexpected T_THROW
any suggestions?
Re: timeout problem, cannot connect to db
Posted: Thu May 21, 2009 3:25 pm
by Darhazer
I'm not sure that this is the place, because the message is different.
However you can rewrite the code as:
Code: Select all
$result = stream_wrapper_register("xlsfile", "xlsStream");
if (!$result) throw new Exception('message');
Re: timeout problem, cannot connect to db
Posted: Fri May 22, 2009 3:50 am
by eatspinach
ya it wasn't that particular line, but i just wanted to remove all die() functions just in case, thanks for all your help the problem appears to be gone away.
i also made some other code changes that could of been causing the problem, like
1. In a function get_all_calls() there was a connect to db() but their was no close db() so I put one in.
2. Everywhere there was a $mysqli->close(); I put a $mysqli->kill($mysqli->thread_id); before it to kill the tread as well.

Re: timeout problem, cannot connect to db
Posted: Fri May 22, 2009 4:00 am
by onion2k
Connecting to a database is a very slow process. If you're opening and closing connections every time you call a function your website is not going to be very fast.