ashlliza
ashlliza

Reputation: 41

Mysql Connection errors

Following code is my config.php

<?php 
    $sDbHost = 'localhost';
    $sDbName = 'liodir';
    $sDbUser = 'root';
    $sDbPwd =    '';
    $dbConn = mysql_connect($sDbHost, $sDbUser, $sDbPwd) 
                     or die('MySQL connect failed: '. mysql_error());
    mysql_select_db($sDbName,$dbConn) 
             or die('Cannot select database: '. mysql_error());
?>

It gives the following error

Warning: mysql_connnect(){function_mysql_connect}: Too many connections in config.php on line 6

it doesnt allow any page to open. please give some solution.

Upvotes: 0

Views: 1977

Answers (5)

Fiido93
Fiido93

Reputation: 1978

You can try this

 <?php
    $sDbHost = "localhost";
    $sDbName = "liodir";
    $sDbUser = "root";
    $sDbPwd = "";

    // Create connection
    $conn = mysqli_connect($sDbHost , $sDbName , $sDbPwd );

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
    ?>

or using your code

<?php 
    $sDbHost = 'localhost';
    $sDbName = 'liodir';
    $sDbUser = 'root';
    $sDbPwd =    '';
    $dbConn = mysqli_connect($sDbHost, $sDbUser, $sDbPwd, $sDbName);

    // Check connection
    if (!$dbConn) {
        die("Connection failed: " . mysqli_connect_error($dbConn));
    }
    echo "Connected successfully";  
?>

easy to understand.

Your second question you told me that your browser doesn't display any error.

Here a way to fix this

  1. go to your folder and find php.ini then find error_reporting = 0 after that change it to error_reporting = E_ALL

Hope this help regards~

Upvotes: 0

Hitesh Mundra
Hitesh Mundra

Reputation: 1588

As @Frank already said, this is showing mysql server have enough clients open. You can't more. I will suggest you-

  1. First don't use mysql_connect(), It's deprecated. Use mysqli_connect().
  2. Second close the connection after used it.

// Here is an example

// create the connection  
 $con = mysqli_connect("HostName","UserName","password","DBName") or die("Some error occurred during connection " . mysqli_error($con));  

// Write query

$strSQL = "SELECT username FROM MyTable";

// Execute the query.

$query = mysqli_query($con, $strSQL);
while($result = mysqli_fetch_array($query))
{
  echo $result["username"]."
";
}

// Close the connection
mysqli_close($con);

?>
  1. Third set the max connections appropriate value in /etc/mysql/my.cnf.
  2. Forth if you have command prompt you can view how many processes have no longer used, and kill them. Following query shows no of client connections running or opened.

mysql> show processlist \G

*************************** 1. row ***************************

     Id: 1
   User: root
   Host: localhost:58834
     db: NULL
Command: Query
   Time: 0
  State: init
   Info: show processlist
1 row in set (0.00 sec)

Upvotes: 0

Mubin
Mubin

Reputation: 4425

try this

 <?php 
    $sDbHost = 'localhost';
    $sDbName = 'liodir';
    $sDbUser = 'root';
    $sDbPwd =    '';
    $dbConn = mysqli_connect($sDbHost, $sDbUser, $sDbPwd, $sDbName) or die('MySQLi connect failed. ' . mysqli_error($dbConn));  
?>

EDIT

at the end of file(or if you'r switching control, before switching control to other page), close the connection as well.

mysqli_close($dbConn);

you need to look here and here

However the default is 151

Upvotes: 1

ashlliza
ashlliza

Reputation: 41

Now the browser is not displaying an error with my file, but in future it can happen again. please give a permanent solution.

Upvotes: 0

Frank van Wensveen
Frank van Wensveen

Reputation: 562

As the error message suggests, you are attempting to open a connection to a database that already has the maximum number of connections open.

Assuming that you are connecting to your own development database and are its only user, and that max_connections and max_user_connections in the my.cnf file are set to sensible values, the next thing to check is whether or not your PHP code closes its database connection(s) before opening (a) new connection(s). Most likely your database has a bunch of open connections that your code has left open. Run your program often enough, and you max out your maximum number of database connections.

Personally I prefer to use mysql_pconnect() to make a persistent connection which stays open and does not require calling more than once during the init state of my programs.

Upvotes: 2

Related Questions