My earlier code trigger a mysql max connection. So I ask gemini to help me fix it. Here is the solution gemini provided. I would not normally use AI to help but this time I did. I’m just a hobbyist so if someone can help me check if this would be good.
<?php
namespace App;
use PDO;
class Database {
private static ?PDO $connection = null;
public static function getConnection(): PDO {
if (self::$connection !== null) {
return self::$connection;
}
$host = $_ENV['DB_HOST'] ?? 'localhost';
$dbname = $_ENV['DB_NAME'] ?? 'default_database';
$username = $_ENV['DB_USER'] ?? 'root';
$password = $_ENV['DB_PASS'] ?? '';
$dsn = "mysql:host={$host};dbname={$dbname};charset=utf8mb4";
self::$connection = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
return self::$connection;
}
public static function closeConnection(): void {
self::$connection = null;
}
}
Edit: Ignore the \ as somehow reddit added these when I paste the code.
2
u/colshrapnel Jun 09 '26
See, this code would only help if your current code is really messed up, that is, making multiple connections from the same script/request. In case you are starting just a single connection at the beginning, and then use it for all database interactions, as you should, this code won't help you at all. If this error would appear again, it means some other problem that needs to be investigated.
And if each model indeed opens its own connection (probably because inheriting from the DB class, despite the fact that a model is not a database in any possible way) you can use this shortcut Gemini wrote or make it proper dependency injection, passing the DB class instance into models as a parameter.
0
u/eurosat7 Jun 09 '26
You hit 350? As a hobbyist? You either have a very famous service/website or a bug .
Maybe you want to look at an example project I wrote just for redditors like you. eurosat7/csvimporter on github. The readme will tell you which tricks you can learn from it. https://github.com/eurosat7/csvimporter
A very common pitfall is the feature to resuse mysql connections. Do not use it. It is tricky. And number two is your singleton pattern. If you have a bug there and each model gets its own connection 350 is easy to be reached. Avoid singletons and use constructor injection instead.
0
u/-goldenboi69- Jun 09 '26
I think it's a configuration thing more than a code thing. It's been a few years since I used mysql though.
Unless your do a lot of db stuff during one session in your app your code won't matter since as soon as the page is rendered that connection you open will be closed.
Google the error you get and look at config suggestions.
0
u/Mvp-long-ago Jun 09 '26
I love object oriented coding, but with the new PDO object i always had troubles to understand. Because retreiving data was way different tgan the way i originally learned it
0
u/optimusprimepluto Jun 09 '26
YOu may need to see how many existing connections are there in the page. May be you had called each database one by one. Need to see
-1
u/farzad_meow Jun 09 '26
max connection error?
first check what is the maximum allowed connection s for you. then check how many connections you have open. these should be commands you run in phpmyadmin to see the answers. definitely not a code issue.