Jump to content

Archived

This topic is now archived and is closed to further replies.

freesouljah

mysql_connect - retry connection

Recommended Posts

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):


[code] 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));
}
}[/code]

I looked around and fiddled with some stuff using tips/ideas from [url=http://gallery.menalto.com/node/39770]here[/url] and [url=http://www.aota.net/forums/showthread.php?postid=28603#post28603]here[/url]...but I can't get it to work for me...

can you help a brother out?

thanks
8)

Share this post


Link to post
Share on other sites
i do mine a lil diff but here would be my sample code

[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));
}
}
[/code]

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

edited to your needs

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
lol....I need it to retry [i]only[/i] 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)

Share this post


Link to post
Share on other sites
Based on the [url=http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html]mysql error codes[/url] 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.
[code]
<?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?
        }
    }
}
?>
[/code]

I had to test this because [url=http://www.php.net/mysql_errno]mysql_errno()[/url] 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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.