timeout problem, cannot connect to db

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
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

timeout problem, cannot connect to db

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: timeout problem, cannot connect to db

Post 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
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Re: timeout problem, cannot connect to db

Post 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();
    }
 
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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: timeout problem, cannot connect to db

Post 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.
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Re: timeout problem, cannot connect to db

Post 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?
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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: timeout problem, cannot connect to db

Post 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');
eatspinach
Forum Newbie
Posts: 20
Joined: Mon Apr 20, 2009 8:50 am

Re: timeout problem, cannot connect to db

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

:drunk:
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: timeout problem, cannot connect to db

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