do...while loops forever

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
While attempting to alleviate a persistent but not consistent problem
with my database operation (DB2) I coded the do...while loop around my
odbc_connect. When $dbConn is false the first time, the program never
returns. This problem repeatable with or without the @ on the
odbc_connect call. I would someone pointing out my stupidity. I did
RTFM, including the examples, and was no further enlightened. Thanks in

function conn() {
     global $schema, $database;
     $schema = "is3";
     $database = "animalst";
     $dbuser = "db2inst1";
     $dbpwd = "db2rules";
     $ct = 0;
     do {
         $dbConn = odbc_connect($database,$dbuser,$dbpwd);
         $ct += 1;
         if($dbConn==False) { sleep(1); }
     while(($dbConn==False) && ($ct<5)) ;
     if ( $dbConn==False) {
         printf("<br>The odbc_connect parameters were:[%s] [%s]
[%s]<br>\n" , $database,$dbuser,$dbpwd);
         printf("<br>Tried %d times<br>\n",$ct);
         die("could not connect to $database");
     return $dbConn;

Re: do...while loops forever

Robert Stearns wrote:
Quoted text here. Click to load it

This might work out better (although I must point out that you should fix
your database if you really need a routine like this):

while (!($dbConn = odbc_connect($database,$dbuser,$dbpwd)) &&
          (++$cnt < 5)) {


Re: do...while loops forever

Quoted text here. Click to load it

As far as I can tell nothing in the PHP code should cause the loop to go
forever. Looks more like a ODBC driver issue to me. The driver must have
acquired some DB2 resouces before it failed, so that when you call
odbc_connect the second time, it deadlocks.

Site Timeline