Jim R Posted January 2, 2020 Share Posted January 2, 2020 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); } Quote Link to comment Share on other sites More sharing options...
requinix Posted January 2, 2020 Share Posted January 2, 2020 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. Quote Link to comment Share on other sites More sharing options...
Jim R Posted January 2, 2020 Author Share Posted January 2, 2020 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. Quote Link to comment Share on other sites More sharing options...
Jim R Posted January 2, 2020 Author Share Posted January 2, 2020 (edited) 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 January 2, 2020 by Jim R Quote Link to comment Share on other sites More sharing options...
Jim R Posted January 2, 2020 Author Share Posted January 2, 2020 Never mind. I figured it out. I had team_profile() in the team_profile function. It was creating an endless loop that was only halted by the connections error. I'm an idiot! Quote Link to comment Share on other sites More sharing options...
Barand Posted January 2, 2020 Share Posted January 2, 2020 5 hours ago, Jim R said: include(ABSPATH ."resources/con.php"); I notice the above line in your function - is that creating a connecting to your database? appearing in every function? Quote Link to comment Share on other sites More sharing options...
Jim R Posted January 4, 2020 Author Share Posted January 4, 2020 (edited) On 1/2/2020 at 5:06 AM, Barand said: I notice the above line in your function - is that creating a connecting to your database? 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 January 4, 2020 by Jim R Quote Link to comment Share on other sites More sharing options...
Barand Posted January 4, 2020 Share Posted January 4, 2020 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. Quote Link to comment Share on other sites More sharing options...
Jim R Posted January 4, 2020 Author Share Posted January 4, 2020 (edited) 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 January 4, 2020 by Jim R Quote Link to comment Share on other sites More sharing options...
Barand Posted January 4, 2020 Share Posted January 4, 2020 16 minutes ago, Jim R said: referring to the function Define. 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.