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) Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/ 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 Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67702 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) Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67715 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 Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67719 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. Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67723 Share on other sites More sharing options...
BillyBoB Posted August 2, 2006 Share Posted August 2, 2006 i edited my code again try taht Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67726 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) Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-67741 Share on other sites More sharing options...
freesouljah Posted August 3, 2006 Author Share Posted August 3, 2006 *bump* ;) Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-68488 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 Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-68564 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) Link to comment https://forums.phpfreaks.com/topic/16306-mysql_connect-retry-connection/#findComment-69728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.