mikefrederick Posted December 19, 2008 Share Posted December 19, 2008 I just launched a new site on bluehost and I have gotten the error: mysql_connect()...Too many connections in... Bluehost says the max connections is 30 and I don't have access to change that. All of my files close database connections at the end of the file. That being said, I don't have many users yet and this problem worries me. Any insight/suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/ Share on other sites More sharing options...
mmarif4u Posted December 19, 2008 Share Posted December 19, 2008 Thats is the problem with the hosting sites, i also face this type of problems before. May be there is no problem in your scripts. If you are using free hosting. then sometime they stop multiple connections for the sake of their servers. I think this should be the reason for you too. Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719469 Share on other sites More sharing options...
RussellReal Posted December 19, 2008 Share Posted December 19, 2008 do not use more than 1 connection? reuse the same connection, that way you will eliminate the error.. there is no reason to have more than 1 database.. maybe more than 1 table for 1 purpose, but more than 1 database for 1 purpose, is excessive, and pointless.. so.. using only 1 mysql connection = actually more effective also. I used to open connections whenever I needed 1, instead of reusing already open ones.. also, this error isn't you opening and not closing.. because @ the very end of php, resources, variables, and any other created data is destroyed, unless you're in some older php version or something. so mysql_close() was really intended for mid-execution resource disposal.. shorten your amount of mysql_connect() to a lower number of calls, if not to 1.. heres an idea: just have a connect.php and inside do your mysql_connect() whatever then at the very top of each page, do include("connect.php"); then every other include below it has access to the connection resource.. thus completely eliminating the need for more than 1 mysql_connect() Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719626 Share on other sites More sharing options...
jordanwb Posted December 19, 2008 Share Posted December 19, 2008 Couldn't you also use mysql_pconnect and never close? Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719630 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 The issue you are having is common if your site recently generated a lot of traffic. Especially with free/cheap hosts it is even more seen cause they do not care to fix it unless you pay more. If you had a dedicated you can change the max number of MySQL connections easily and it fixes the problem. I had this when my site started getting 10,000+ hits a day and had to change the max connections in MySQL and have not had that problem since. Unfortunately without a dedicated server you will have to ask them to change it, and they probably will not. Oh and the pconnect, I do not think would help, because it seems he has more than a few hundred users that connect at a time, which is what is causing the issue. This is assuming you are really only opening one connection per page and no more. If you are opening more than 1 per each page, do as Russell said and re-design your script to use 1 database call per connection. Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719685 Share on other sites More sharing options...
jordanwb Posted December 19, 2008 Share Posted December 19, 2008 Oh and the pconnect, I do not think would help, because it seems he has more than a few hundred users that connect at a time, which is what is causing the issue. Oh. This is assuming you are really only opening one connection per page and no more. If you are opening more than 1 per each page, do as Russell said and re-design your script to use 1 database call per connection. May I suggest creating a class that connects to MySQL and storing the class in $_SESSION? Then putting mysql_close in the the __destruct function? Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719900 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 Oh and the pconnect, I do not think would help, because it seems he has more than a few hundred users that connect at a time, which is what is causing the issue. Oh. This is assuming you are really only opening one connection per page and no more. If you are opening more than 1 per each page, do as Russell said and re-design your script to use 1 database call per connection. May I suggest creating a class that connects to MySQL and storing the class in $_SESSION? Then putting mysql_close in the the __destruct function? mysql_close is unnecessary as russell pointed out php closes connections when the script exits. As for the class in session, that is fine, but you still have to re-instantiate/connect to the DB each time a page is loaded. Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-719906 Share on other sites More sharing options...
RussellReal Posted December 20, 2008 Share Posted December 20, 2008 also note, I've only EVER worked with VDS/DS and currently own 2 VDS hosting plans, 1 with jumpline (which i really wouldn't recommend expensive and SHIT) and 1 with WestHost I got it at the $10 a month deal, and imo its really worth it lol Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-720233 Share on other sites More sharing options...
mikefrederick Posted December 21, 2008 Author Share Posted December 21, 2008 I don't think the site has had 30 visitors at once thus far, and each file has: require 'connectionfile.php' at the top and does not open any other connections except for in that file. Weird right? Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-720909 Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 try <?php require_once(""); ?> Interesting. Quote Link to comment https://forums.phpfreaks.com/topic/137651-too-many-mysql-connections/#findComment-720925 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.