timeout problem, cannot connect to db
Moderator: General Moderators
-
eatspinach
- Forum Newbie
- Posts: 20
- Joined: Mon Apr 20, 2009 8:50 am
timeout problem, cannot connect to db
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
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
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
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
-
eatspinach
- Forum Newbie
- Posts: 20
- Joined: Mon Apr 20, 2009 8:50 am
Re: timeout problem, cannot connect to db
Thanks for your reply, I am still a bit confused.
Here is a more detailed view of my code,
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,
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();
}
Last edited by Benjamin on Wed May 20, 2009 8:01 am, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
Re: timeout problem, cannot connect to db
Probably in the print_call_log() or in a function, called by it, there is a die() or exit()
for example:
Replace the die() with:
So you can catch the exception and continue the execution of the script.
for example:
Code: Select all
mysql_connect(...) or die("error cannot connect to the database")'Code: Select all
throw new Exception('Cannot connect to database');-
eatspinach
- Forum Newbie
- Posts: 20
- Joined: Mon Apr 20, 2009 8:50 am
Re: timeout problem, cannot connect to db
I did find this line that could be causing the problem,
I am unable to replace the die function with a throw function,
I tried this
But that didn't work, I just got this error
Code: Select all
stream_wrapper_register("xlsfile", "xlsStream")
or die("Failed to register protocol: xlsfile");
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
any suggestions?Parse error: syntax error, unexpected T_THROW
Last edited by Benjamin on Thu May 21, 2009 8:40 am, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
Re: timeout problem, cannot connect to db
I'm not sure that this is the place, because the message is different.
However you can rewrite the code as:
However you can rewrite the code as:
Code: Select all
$result = stream_wrapper_register("xlsfile", "xlsStream");
if (!$result) throw new Exception('message');-
eatspinach
- Forum Newbie
- Posts: 20
- Joined: Mon Apr 20, 2009 8:50 am
Re: timeout problem, cannot connect to db
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.

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
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.