Jump to content

mysql_connect - retry connection


freesouljah

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.