Ricky55 Posted February 17, 2012 Share Posted February 17, 2012 Hi I'm wondering if any of you guys would be kind enough to help me with this. A developer that I use is away on holiday and I'm having a few connection issues. From time to time I get the two following errors 1. Lost connection to MySQL server at 'reading initial communication packet', system error: 111 2. Too many connections We are currently using this code: <?php $conn = conn(); function conn($host='79.170.**.**', $user='web140-ffc', $pwd='XXX', $db='web140-ffc') { $conn = mysql_connect($host, $user, $pwd) or die ("<h1>mysql_connect Error</h1>\n <ul>\n <li>Error in: " . $_SERVER['PHP_SELF'] . "</li>\n <li>Selecting database : $db</li>\n <li>With username: $user</li>\n <li>And Password: $pass</li>\n <li>mysql_error: " . mysql_error() . "</li>\n </ul>\n"); mysql_select_db($db, $conn) or die("<h1>mysql_select_db Error</h1>\n <ul>\n <li>Error in: " . $_SERVER['PHP_SELF'] . "</li>\n <li>Selecting database : $db</li>\n <li>With username: $user</li>\n <li>And Password: $pass</li>\n <li>mysql_error: " . mysql_error() . "</li>\n </ul>\n"); return $conn; } function qry($sql) { $result = mysql_query($sql) or die("<h1>Error Querying Database</h1>\n<ul><li>SQL: $sql</li>\n<li>Error: " . mysql_error() . "</li>\n</ul>"); return $result; } ?> Then on every page I have an include to this connection file <?php include 'scripts/func.php'; ?> I was wondering if you could take a look and tell me A. How this code looks, can you see anything that might be causing the issue? B. Is there anyway to prevent the Too Many connections error? perhaps increase the number of connections or have a check in place? Thanks in advance Quote Link to comment Share on other sites More sharing options...
shlumph Posted February 17, 2012 Share Posted February 17, 2012 A. How this code looks, can you see anything that might be causing the issue? From what you've shown, I don't see anything that could be causing the problem. B. Is there anyway to prevent the Too Many connections error? perhaps increase the number of connections or have a check in place? Is the 'scripts/func.php' included multiple times per request? Namely, is conn() executed multiple times per request? There should only be one DB connection per request, if all your queries are going to the same DB host. This is the first thing I would check. If that's not the case, you can always increase the number of connections: http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted February 17, 2012 Author Share Posted February 17, 2012 Thanks for replying. the scripts/func.php is just included at the top of every page of the website. Does this answer your question? Does this mean that this connection will be made every time a new page is loaded? Could this be done in a more efficient manner? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 20, 2012 Share Posted February 20, 2012 You're moving to the world of persistent connections. Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted February 20, 2012 Author Share Posted February 20, 2012 Sorry Fenway! I'm totally new to mySQL I don't know how to get this information. Would I be better using a persistent connection? or do you need this info first? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 20, 2012 Share Posted February 20, 2012 Usually, mysql connections are not the speed issue compared to everything else, so I wouldn't bother. Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted February 20, 2012 Author Share Posted February 20, 2012 Looking at the code I provided is there anything else you can think of that I could get my dev to check for me? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 22, 2012 Share Posted February 22, 2012 Too many connections -- how are many is the max? 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.