Reputation: 41
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
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
php.ini
then find error_reporting = 0
after that change it to error_reporting = E_ALL
Hope this help regards~
Upvotes: 0
Reputation: 1588
As @Frank already said, this is showing mysql server have enough clients open. You can't more. I will suggest you-
// 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);
?>
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
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
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
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