freesouljah Posted August 2, 2006 Share Posted August 2, 2006 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) Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted August 2, 2006 Share Posted August 2, 2006 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 Quote Link to comment Share on other sites More sharing options...
freesouljah Posted August 2, 2006 Author Share Posted August 2, 2006 thanks for the speedy response ;Dthis 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) Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted August 2, 2006 Share Posted August 2, 2006 um can u edit it cuz i dont really get wat your saying Quote Link to comment Share on other sites More sharing options...
freesouljah Posted August 2, 2006 Author Share Posted August 2, 2006 original post edited for clarification.mainly I need it to safeguard from a 'too many connections' error. Quote Link to comment Share on other sites More sharing options...
BillyBoB Posted August 2, 2006 Share Posted August 2, 2006 i edited my code again try taht Quote Link to comment Share on other sites More sharing options...
freesouljah Posted August 2, 2006 Author Share Posted August 2, 2006 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... :-Xthank you for your patience and help 8) Quote Link to comment Share on other sites More sharing options...
freesouljah Posted August 3, 2006 Author Share Posted August 3, 2006 *bump* ;) Quote Link to comment Share on other sites More sharing options...
shoz Posted August 3, 2006 Share Posted August 3, 2006 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]<?phpfunction 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 Quote Link to comment Share on other sites More sharing options...
freesouljah Posted August 5, 2006 Author Share Posted August 5, 2006 right on man....thank you much 8) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.