I wonder if somebody can help me please, I keep getting a ‘max_user_connections’ I think because I’m not closing the connection. At the moment I can’t check my script and just wanted to ask if this okay going to work or if I’m doing something wrong?
Persistent connections come from requesting too much from the database. If you are on a free hosting provider, you will see this because they typically disable more than one connection since they want you to pay for more service.
I recommend going back and redoing your code. I can see that you are mix matching mysqli_* and mysql_*. Please don’t do this as it may cause problems later in the future.
I’m paying for my hosting and with the package it says I get 20 x1gb databases (I’ve only used 2% of one of them) with low contention (I don’t know what that means) so I don’t think that’s the case, or at least I hope that’s not the case.
I’m need to request a lot of information from the database because I’m working on a site that compares various products so there’s a lot of filters that people can search via. Without any of those filters working I’m using this to get the results from the database (multiple tables need to be searched):
$query = "SELECT * from feeds, product_categories_map, timing, flavour, source, dietary, ingredients, sweeteners, type WHERE %s %s %s feeds.price >=%s and feeds.price <=%s and feeds.enabled=1 and feeds.stock=1 and feeds.deleted=0 GROUP BY feeds.product_name ORDER BY `%s` %s %s";
$query = sprintf($query, $brand_added, $weight_added, $freesearch, $_GET['budgetFrom'], $_GET['budgetTo'], $_GET['sortedBy'], $_GET['sortDirection'],$sql_limit);
$product_results=dbselect( $query,"dbLinkInt" );
I’ve changed the mysql_close() to mysqli_close() now so hope that will prevent the connection from being open?
I’m getting desperate to get this working and am really grateful for you helping with this.
Don’t close the database connection. Let PHP do this for you. It’s not good practice to keep opening a new connection every time you want to make a database call. I think the problem actually likes in the logic of the code.
Don’t be. The end result will be more satisfying if you don’t rush it. The more you rush it, the more errors you’ll end up trying to fix.
Can you please provide more code? It would be more helpful seeing what is causing the max persistents.
That query looks completely wrong and I don’t think it does what you think it does. I’m fairly certain since you’re not joining on anything all the data is being exponentially duplicated.
It is, but the code uses sprintf() to replace the values, possibly just because it would allow order, limit and so on to be specified. A bit scary that it uses some user-supplied values without validation.
I don’t see much problem other than the sprintf function being used. It’s better off to use either PDO or mysqli_ to deal with database calls. Do you access to your php.ini file?
If it’s a possibility, I’d say to switch to one that supports this because it may be a solution to your max persistent connections. I had the same error a while ago. I fixed mine by setting it in my php.ini file.