Jump to content

Max User Connection error...can't connect to mysql database


Recommended Posts

On one of my sites, I have created a number of custom functions to put into my WordPress installation.  Suddenly I'm getting a max user connection error.  

Here is the page it's showing:

https://www.courtsideindiana.com/tag/brownsburg/

The right hand side of the page are two of my custom functions.  The content above Tag:  Brownsburg is a custom function, which is supposed to just have test content.  The odd behavior also is there should just be one Brownsburg listed and one Team1 (not 23).

I have tried to close each connection

mysqli_close($con);        

If it would matter, the query is below:  

function team_profile() {

//
//
////////// begin team profile
//
//

	include(ABSPATH ."resources/con.php");


		// Tag or Search

		if (function_exists( 'single_tag_title' )) { 
    		$tag = single_tag_title("", false);
    		
		} 
		 else {
		 	$tag = get_search_query();
		 }

		echo $tag .'<br>';  // just to test

		$query = "SELECT * FROM a_schools
				WHERE (school = '" .$tag . "' || concat(city,' ',school) = '".$tag."')";
		
		// End tag vs search term, finish the query

		$results = mysqli_query($con,$query);
		echo mysqli_error($con);
		if($line = mysqli_fetch_assoc($results)) {
		
		$school = "{$line['city']} {$line['school']}";
					
			if($line['school'] == $tag) {
				team_profile();
				echo 'Team 1';  // just to test
			}
			else {
				team_profile();
				echo 'Team 2';  // just to test
			}
			
				
		}

mysqli_close($con);		
}

 

Maybe you're creating and recreating connections multiple times per script. Maybe your queries are taking too long. Maybe something else. Hard to say.

Add and remove your new functions until you find out which one is causing the problem.

1 hour ago, requinix said:

Maybe you're creating and recreating connections multiple times per script. Maybe your queries are taking too long. Maybe something else. Hard to say.

Add and remove your new functions until you find out which one is causing the problem.

I think the 23 instances of Brownsburg and Team1 is what is triggering the error.  When I remove the query and establish and echo $tag it works fine.  

I don't see anything in the query that would lead to 23 instances of Brownsburg and Team1.

 

 

This function determines if we have a Player's name or Team

 

function team_or_player () {

///
///
///  Determine if it's a player or a team or player profile
///
///  This function is located in the /plugins/td-standard-pack/newspaper/tag.php
///
///

		// tag or search
			
		 if (function_exists( 'single_tag_title' )) { 
    		$tag = single_tag_title("", false);
		} 
		 else {
		 	$tag = get_search_query();
		 }

 include(ABSPATH ."resources/con.php");

		$query = "SELECT nameFirst,nameLast,city,school,position,grade FROM a_players
				WHERE CONCAT(nameFirst,' ', nameLast) = '".$tag."'";			

		$results = mysqli_query($con,$query);
		echo mysqli_error($con);
		if($line = mysqli_fetch_assoc($results)) {
		
			$name = "{$line['nameFirst']} {$line['nameLast']}";
			echo $name;  // Just to make sure it's getting the intendeded result
			
			player_profile();
		}
		else {
				echo 'Team';  // Just to make sure we're getting passed to Team
				team_profile();
		}

///
////// End Team or Player
///		
		
mysqli_close($con);		
}

Passing it to the Player Profile works just fine.  Passing it to the Team Profile (code in the OP) works, but the query produces 23 instances.  

Edited by Jim R
5 hours ago, Jim R said:

include(ABSPATH ."resources/con.php");

I notice the above line in your function - is that

  1. creating a connecting to your database?
  2. appearing in every function?
On 1/2/2020 at 5:06 AM, Barand said:

I notice the above line in your function - is that

  1. creating a connecting to your database?
  2. appearing in every function?

Yes, it's connecting the function to the database, appearing in every function, but what was causing my issue at hand was me creating an endless loop and it ending at my server connections limit.  I tried referring to a global connection, but I didn't get it to work.

I'm closing every connection at the end of each function.  

Edited by Jim R

Extremely inefficient. Connecting to the db server probably takes longer than the query so that's quite an overhead you are adding to your script.

You should connect once to the server at the start of the script and pass the connection as a parameter to any functions that need it. The connection will close automatically when the script finishes its execution.

20 minutes ago, Barand said:

Extremely inefficient. Connecting to the db server probably takes longer than the query so that's quite an overhead you are adding to your script.

You should connect once to the server at the start of the script and pass the connection as a parameter to any functions that need it. The connection will close automatically when the script finishes its execution.

Can I do that via a function on the same file and referring to the function?

Edited by Jim R
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.