IT.COM

Help - Max number of connections - Mysql

Spaceship Spaceship
Watch

Ik

Quality //VIP Member
Impact
8
Even though I make sure I close all the DB connections in my code, I had this message displayed on my site today!!!!!

user cvs4009 already has more than "max_user_connections" active connections in /home/public_html/core.php on line 601

Pls help
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
What is the current maximum number of connections?

Also is there more than 1 application connecting using this username (regardless of the actual database they are accessing).
 
0
•••
Hello Peter,

The warning message didn't indicate a number. There is only one user/site accessing this database.

Here is 2 samples of my code:

Code 1

Code:
function find_email($e){
	include('config.php');
	$connection = mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname,$connection);
	$sql = "SELECT id from customers where email = '" . $e . "'";
	$result = mysql_query($sql) or die('error, query failed');
	$num = mysql_num_rows($result);
	mysql_close($connection);
	$i=0;
	$id = 0;
	if($num > 0){
		while ($i < $num){
			$id = mysql_result($result,$i,"id");
			$i++;
		}
	}
	return $id;
}

Code 2 (using a pagination class)

Code:
function products($id,$para){
	echo $ids;
	require('config.php');
	require_once('ps_pagination.php');
	$connection = mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname,$connection);
	$sql = "SELECT name, code, public, image from products where online = 1 and categoryid in (" . $id . ") order by id desc";
	$q = 'category='.$para;
	mysql_close($connection);
	$pager = new PS_Pagination($connection, $sql, 10, 5,$q);
	$rs = $pager->paginate();
	$i = 0;
	$foundEntries = false;
	if(strlen($rs) > 0){
		$foundEntries = true;
		while($row = mysql_fetch_assoc($rs)){
			echo '<div class="productbrief"><div class="productbriefimage"><a href="details.php?public=';
			echo $row['public'] . '">';
			if(strlen($row['image']) > 0){
				echo '<img src="data/images/' . $row['image'] . '" width="70" />';
			}
			else{
				echo '<img src="images/noimage.jpg" width="70" />';
			}
			echo '</a></div><div class="productbrieftext"><em><a href="details.php?public=' . $row['public'] . '">' . $row['name'] . '</a></em><br /><em>Product #:</em> ' . $row['code'] . '</div><div class="clr"></div></div>';
			if((($i+1) % 4) == 0){
				echo '<div class="clr"></div>';
			}
			$i++;
		}
		echo '<div class="clr"></div>';
		echo '<div class="pager">' . $pager->renderFullNav() . '</div>';
	}
	return $foundEntries;
}
 
0
•••
The error you receive is not about the single database it is about the user. If you have 2 databases that the same user can access this would result in the same issue.

I notice from your code snippets that these 2 functions call the database and make a new connection. This is very ineeficient and you will find the script making a fresh connection to the DB quite a lot. For example if you called each of those functions 5 times each on the page that is 10 database connections just for the 1 page. Now multiply that by the amount of visitors and it grows exponentially.

You should be opening the database and leaving the 1 connection open until the script has finished with the database completely.
 
0
•••
The error you receive is not about the single database it is about the user. If you have 2 databases that the same user can access this would result in the same issue.

I notice from your code snippets that these 2 functions call the database and make a new connection. This is very ineeficient and you will find the script making a fresh connection to the DB quite a lot. For example if you called each of those functions 5 times each on the page that is 10 database connections just for the 1 page. Now multiply that by the amount of visitors and it grows exponentially.

You should be opening the database and leaving the 1 connection open until the script has finished with the database completely.

Hi Peter,

Thanks for the information. Then is it a good solution to open a connection when my PHP page loads, and then close it when the page finish loading??
 
0
•••
Yes that would be a better solution than you have at the moment.
 
0
•••
Thanks Peter,

The information you've given is very helpful.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back