Jump to content


Photo

mysql_connect - retry connection


  • Please log in to reply
9 replies to this topic

#1 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 02 August 2006 - 09:23 AM

okay...I am trying something that should be simple...but I can't get it to work...I want this php script to make sure it connects to mysql...for instance if there are too many connections, it retries (maybe 5 times, with a 1 second lapse between, and then stops if it cannot connect at that point):


function dbConnect ( ) { 
	
		if ($this->dbconnection == "") {
			$this->dbconnection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true);
			mysql_select_db($this->dbname, $this->dbconnection);
			print (mysql_error($this->dbconnection));
		}
	}

I looked around and fiddled with some stuff using tips/ideas from here and here...but I can't get it to work for me...

can you help a brother out?

thanks
8)

#2 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 09:29 AM

i do mine a lil diff but here would be my sample code

function dbConnect ( ) {
if($this->dbconnection == ""){
while($i!=5){
$this->dbconnection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true);
mysql_select_db($this->dbname, $this->dbconnection);
sleep(1);
$i++
}
if($this->dbconnection == ""){
print (mysql_error($this->dbconnection));
}
}

now most of that is based off your code i dont do the exact same way!

edited to your needs

#3 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 02 August 2006 - 09:54 AM

thanks for the speedy response  ;D

this works, but what I need it to do is to only attempt to retry connection if the first and/or subsequent connections fail...I guess I should have been a bit more clear...

thanks
8)

#4 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 10:01 AM

um can u edit it cuz i dont really get wat your saying


#5 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 02 August 2006 - 10:05 AM

original post edited for clarification.

mainly I need it to safeguard from a 'too many connections' error.

#6 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 10:16 AM

i edited my code again try taht

#7 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 02 August 2006 - 10:35 AM

lol....I need it to retry only if there is a problem connecting (ie 'too many connections)...leave it to me to have to clarify my clarification...  :-X

thank you for your patience and help
8)

#8 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 August 2006 - 12:27 PM

*bump*

;)

#9 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 03 August 2006 - 02:27 PM

Based on the mysql error codes listing, error number 1040 should be for the general "too many connections" error and 1226 applies for a number of errors including a case where the specific user has currently used more connections than they've been allowed.

The error codes are being used so that you're not trying 5 times to make a connection that has other problems.
<?php
function dbConnect()
{
    if ($this->dbconnection == "")
    {
        for ($i = 0; $i < 5; $i++)
        {
            $this->dbconnection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass, true);
            $errno = mysql_errno();
            if ($errno == 1040 || $errno == 1226 || $errno == 1203)
            {
                sleep(1);

            }
            else
            {
                break;
            }
        }
        
        if ($this->dbconnection)
        {
                mysql_select_db($this->dbname, $this->dbconnection);

        }
        else
        {
            //return an error possibly?
        }
    }
}
?>

I had to test this because mysql_errno() can take a link_identifier and I wasn't sure how it would deal with a failure on connect. Meaning no link identifier could be given.

EDIT: added an error code

#10 freesouljah

freesouljah
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 05 August 2006 - 10:51 AM

right on man....

thank you much  8)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users