Programmers learn & share
0 votes
76 views

Problem :

I am getting following error in my error log file

[18-Nov-2019 19:09:55 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1040] Too many connections' in /home/root/products/db.php:2

Stack trace: /home/root/products/db.php(2): PDO->__construct('mysql:host=loca...', 'database', 'password')

/home/root/products/by-brand.php(2): include_once('/home/root/...')

{main} thrown in /home/root/products/db.php on line 2

[18-Nov-2019 19:15:11 UTC] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1286 Unknown storage engine InnoDB in /home/root/products/detail.php:8

Stack trace: /home/root/products/name.php(8): PDO->prepare('select * from p...')

{main} thrown in /home/root/products/name.php on line 8

I have tried to solve this issue by closing every script by using $db=null; but it is not working for me.

I also contacted the service provider.

Why am I getting this error?

My db.php file has following code. Can you tell if this is fine?

My db.php as follows:

$db = new PDO('mysql:host=localhost;dbname=mobiles;charset=utf8', 'root', '**somePassword**');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
by (6.9k points)   | 76 views

1 Answer

0 votes

Solution :

This issue is happening due to the limit of database's configuration.If you have the permissions to edit the configuration file of your database service then you can update the max_connections value.

You can also try to run following SQL query (Please Note : take care about this value.)

SET GLOBAL max_connections = 512;

and following is to get current value of all variables:

SHOW GLOBAL VARIABLES LIKE '%max_connections%'

By doing this please try to set PDO object to null when you don't need it. This will help in closing the connection to your database and PHP need not wait for script to finish and after that close active connections.

Further Readings: 

http://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html

by (36.1k points)  
2,245 questions
2,807 answers
60 comments
241 users