Jump to content

Connection newbie blues


Ricky55

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

 

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.