Jump to content

Too Many Connections Nightmare!


jimmyoneshot

Recommended Posts

I'm working on a website and have constantly been getting a "too many connections" error.

 

I know this problem is not caused on my code and it seems if I leave it for a while then come back it goes away but when it occurs it stays for long periods and I can't do a thing about it.

 

What is it exactly that causes this error as it has never happened before and is there anything I can do about it?

 

I have heard it could be a lot of users all on the same hosting service at once as it is shared hosting that I use, is this true or is it perhaps just a scam to get people to upgrade after a certain amount of time?

 

Here's my connection script by the way and I have no idea why this could be happening when I use this type of connection and it's only me that uses the site  :wtf::-

 


$hostname = "localhost";
$database = "*******";
$username = "*******";
$password = "*******";

$myconnection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

Link to comment
Share on other sites

I'm working on a website

 

it's only me that uses the site

 

Are you sure that you are the only one making page requests? Any publicly accessible site is open to abuse. Perhaps some hacker is trying trying to break into your database or break into your script(s).

 

The number of available database connections is (usually) managed on a per hosting account basis, so if you are getting that error, it (should) mean that it is due to your script(s). Have you actually asked your web host to provide information that they know about concerning when that error has been occuring, like how many connections are available and how many your account has been using?

 

Beyond that, are you including any code using a URL (instead of using a file system path) to your own site that is also making a database connection in each piece of code, such as piecing together a web page by including various sections (menu, header, content, footer,...)?

 

 

Link to comment
Share on other sites

I would say you should make a class, and connect that way. That way you can make a db connection once.

 

<?php
class Mysql{
private $conn = null;
private $sql = null;
private $row = null;
public function __destruct(){
	mysql_close($this->conn);
	$this->conn = null;
	$this->sql = null;
	$this->row = null;
}
public function connect($user, $pass, $host, $db){
	if($this->conn == null){
		$this->conn = mysql_connect($user, $pass, $host, $db);
		mysql_select_db($db, $this->conn);
		return $this->conn;
	}
	return false;
}
public function query($query){
	if($this->conn == null)
		return false;
	$this->sql = mysql_query($query, $this->conn);
	return $this->sql;
}
public function row(){
	$this->row = mysql_fetch_assoc($this->sql);
	return $this->row;
}
public function field($field){
	return $this->row[$field];
}
public function num_rows(){
	return mysql_num_rows($this->sql);
}
public function get_one($query){
	$this->query($query);
	$arr = mysql_fetch_array($this->sql);
	return $arr[0];
}
}
?>

 

Then when you use it, you can only connect to the database if there is not an open connection already.

 

So you could do this:

<?php
// One way to use this:
$db = new Mysql();
$db->connect('123','123','123','123');
$db->connect('123','123','123','123'); // This connection will be ignored (returns false)
$db->query("select * from mytable");
while($db->row()){
echo $db->field('column1')."   :   ";
echo $db->field('column2')."<br />";
}

// A second way:
$db = new Mysql();
$db->connect('123','123','123','123');
$db->connect('123','123','123','123'); // This connection will be ignored (returns false)
$first_name = $db->get_one("select first_name from mytable where member_id = 1232");
echo $first_name;
?>

 

Another thing when you use this class, you will want to connect in ONE global file that is used on every page. Also, you almost always want to use require_once, and not include/require/include_once. require_once will include a file, and if the file has been included it won't include it again.

 

You would want to use include if you have repeating code, such as sending a dynamic email to many members where each email is different that the previous one.

 

I don't know if this helps at all. BTW, I didn't test my code.

Link to comment
Share on other sites

Database connections within one script that use the same credentials reuse the first/existing connection without making a new connection unless you specifically tell mysql_connect() to make a new connection (see the 4th mysql_connect parameter.)

 

If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters.
Link to comment
Share on other sites

Skipping over making a connection, if there already is one, isn't relevant to the OP's problem. The code he posted, no matter how many times it might get called within one instance of a script, isn't the cause of the error he is trying to troubleshoot.

 

Your code is also incorrectly using the $db as the 4th parameter to mysql_connect, so multiple instances of your db class within one script will create a new connection every time instead of reusing an existing connection.

Link to comment
Share on other sites

Thanks for the suggestions, I'll try a few of them out.

 

Ok the error suddenly disappeared only to now be replaced by the error:-

 

406 Not Acceptable

 

every time I try to access the site.

 

Not sure if I can mention my server provider here but it is truly awful and is quite a big name one too.

 

On the subject of security can anyone recommend some tips on making a site completely secure?

 

At the moment I've wrapped the whole site in an if statement which checks if a specific session is set. This session is only set when the password is correct. Here is my code for this at the moment. Can anyone recommend improvements please:-

 

<?php

 

session_start();

 

$password = "******"; 

 

if ($_POST['txtPassword'] != $password && !isset($_SESSION['is_logged_in'])) {  ?> 

 

<h1>Login</h1>  <form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">   

<p><label for="txtpassword">Password:</label>    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>      <p><input type="submit" name="Submit" value="Login" /></p>  </form>  <?php  } else { 

 

  $_SESSION['is_logged_in'] = 1;

 

}  ?> 

 

<?php if(isset($_SESSION['is_logged_in'])):?>

 

MY SITE CODE

 

<?php endif;?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.