HI all, I've been getting the following error for some time and can't figure the reason. Please advise. *(42000/1203): User DB2 already has more than 'max_user_connections' active connections in /home7/triveste/public_html/mysql/connect/connect_to_mysql.php on line 19* LINE 19 is --- $ConnComment = mysqli_connect("$db_host","$db_username1","$db_pass1", "$db_name1") or die ("could not connect to mysql"); This is how connections are made $db_host = "localhost"; $db_username = "DB1"; $db_pass = "xxxxxxxx"; $db_name = "DB1"; $db_username1 = "DB2"; $db_pass1 = "xxxxxxxxx"; $db_name1 = "DB2"; $myConnection = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql"); $ConnComment = mysqli_connect("$db_host","$db_username1","$db_pass1", "$db_name1") or die ("could not connect to mysql"); Code (markup): This is an example of the code on each webpage: <?php require_once $_SERVER['DOCUMENT_ROOT'].'/mysql/connect/connect_to_mysql.php'; ?> <?php $sql = 'SELECT * FROM DB1 WHERE id= "187" '; $result = mysqli_query($myConnection, $sql) or die (mysqli_error($myConnection)); $row = mysqli_fetch_array($result) or die(mysql_error()); mysqli_free_result($result); include_once $_SERVER['DOCUMENT_ROOT'].'/inc/template2/headerhtml.php'; ?> <?php $sql = 'SELECT * FROM DB1 WHERE id= "187" '; $result = mysqli_query($myConnection, $sql) or die (mysqli_error($myConnection)); $row = mysqli_fetch_array($result) or die(mysql_error()); mysqli_free_result($result); include_once $_SERVER['DOCUMENT_ROOT'].'/inc/template2/120216-img/body1.php'; ?> <div id="commentsholder"> <div id="otherstories">Other Stories: <ul class="navlist"> <?php $sqlCommand = "SELECT metakey, h1tag, linkurl FROM DB1 WHERE (metakey LIKE '%XYZ%' OR metakey LIKE '%XZY%') AND metakey NOT LIKE '%encedupr%' ORDER BY id Desc LIMIT 3"; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection)); while ($row = mysqli_fetch_array($query)) { $relatedh1 = $row["h1tag"]; $relatedlink = $row["linkurl"]; echo "<li>"; echo '<a href="/' . $relatedlink . '"' . ' title="' . $relatedh1 . '">' . substr_replace($relatedh1,' ...',85) . '</a>'; echo "</li>"; } mysqli_free_result($query); ?> </ul> </div> </div> </div> <div id="contentl2"> <div id="comments"> <?php require $_SERVER['DOCUMENT_ROOT'].'/talkback/comments.php'; ?> </div> </div> <?php require_once $_SERVER['DOCUMENT_ROOT'].'/inc/template2/120216-img/body2.php'; ?> Code (markup):
Hi all, Got the problem partially solved. I was using the same user to connect to two databases. My webhost allows only 15 simultaneous connections, and one user using two databases reduced that number to 7. As there are only 15 connections I will still have to find ways of optimizing as much as possible. Any pointers would be appreciated.
Switch to a singleton database class. This will allow users to share the same connection resource as long as it is already present. Should drastically reduce the number of connections if you are getting multiple at the same time. You may have to create a mysqli wrapper class to use it though.
I have implemented the singleton as suggested and it seems to be working, but when I run the Web Link Validator application I am getting 500 server errors. Any suggestions? The below code calls the code above from each web page.