Barney
Barney

Reputation: 2373

How to prevent exceeding max_user_connections upon reloading/refreshing page multiple times?

I am using a free web hosting service called getfreehosting.co.uk. The max_user_connections is 5. Every time my page loads, the first thing it does is invoke a load.php that fetches an entry from my database. My problem is that if I reload or refresh the page too many times, I get the following error:

Warning: mysql_connect() [function.mysql-connect]: User getfh_11654008 already has more than 'max_user_connections' active connections in /home/vol9/getfreehosting.co.uk/getfh_11654008/htdocs/experiment/load.php on line 3 Could not connect: User getfh_11654008 already has more than 'max_user_connections' active connections

I understand that I'm using a free web host, so I may not be able to increase max_user_connections. I shouldn't have to anyway because I only need to have one instance of my page running at any given time, so in theory max_user_connections = 5 is more than enough for my purposes. At first I thought the issue must be that my load.php was being called too many times, resulting in too many sql_connections that aren't closed. However, I call sql_close() at the appropriate places but still manage to exceed max_user_connections.

Here's my load.php:

<?php
    $link = mysql_connect(MYSQL_HOSTNAME, MY_USERNAME, MY_PASSWORD);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db(MY_DB, $link);

    $result = mysql_query(SELECT ONE ENTRY);
    if($r = mysql_fetch_assoc($result)) {
        print json_encode($r); // convert data to json and return it
        mysql_close($link);
    }
    else {
        mysql_close($link);
        die("Error: something went wrong");
    }
?>

What's wrong and what should I do?

Upvotes: 0

Views: 2220

Answers (1)

Bravo Delta
Bravo Delta

Reputation: 950


ANSWER:

Two things you can look into:

  1. mysql_close() - It might be that the system is waiting a few seconds to close connections because you didn't tell it to. (IE: the computer doesn't close the door right away until you tell it to or until after everything is said and done).

$connection = mysql_connect(); mysql_close($connection);

Make sure to use the variable when connecting...

  1. You could try php's mysql persistent connection. In essence one permanent connection to the database... it isn't ideal, but it could also work. It's syntax is virtually the same as mysql_connect():

$connection = mysql_pconnect("localhost","mysql_user","mysql_pwd");


SECONDARY:

If your interested I may be able to provide you a better php/mysql development environment (for free, with no obligations of any kind)... 100% your choice, just trying to help you out.


RANT:

I don't understand why people on here insist on telling others to use PDO, but don't help solve the problem... people are going to use whatever they want to until they learn a better way to do it. Beating the PDO horse isn't gonna make people adopt it faster...

Upvotes: 3

Related Questions