I am creating a database driven website - and I have the content stored in a mysql database at the moment. At the moment each page starts with the script:

$connect = mysql_connect('$host', '$username', '$password');

$select = mysql_select_db('$database', $connect);

This works fine, but if Iv been playing around with the site for too long, it comes back with an error that too many database connections are open.

I have a few questions:

1) Does the script above open a new connection each time it is executed?
2) If so how can I reuse a connection across pages?
3) If not how would I stop 'too many connections' from being opened?

Many Thanks


13 Years
Discussion Span
Last Post by zippee

1. PHP would close all open connections after the script terminates.
2. -
3. It's probably a cause of some other sites on the same server opening too many connections that the mysql is unable to cope. Are you on shared hosting btw?


No I am on dedicated hosting. The pages on my site normally load quickly, but every now and again, they take way too long to load.


I have been "reading" MySQL manual yesterday...

1. -
2. MySQL allow 4000 (or 400??) simultaneous connections. After you close the database, it will take 3-4 minutes before the connection can be reuse.
3. You can use SSI to open the connection of a page and only close it when leaving the page. Alternatively, but I've never try before, you may use session to open (session start) and close (session end) the connection when someone visiting to and leaving the site, respectively.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.