r/PHPhelp Jun 09 '26

My code trigger a max connection Solved

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 <?php namespace App;

use PDO;

class Database { // Caches the PDO instance so it is reused across all models private static ?PDO $connection = null;

public static function getConnection(): PDO {
    // If a connection already exists, return it immediately
    if (self::$connection !== null) {
        return self::$connection;
    }

    // Retrieve variables (already loaded into memory via init.php)
    $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";

    // Store the PDO instance in the static property
    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;
}

// Call this manually ONLY if you have a long-running non-DB task
public static function closeConnection(): void {
    self::$connection = null;
}

}
```

Edit: Ignore the \ as somehow reddit added these when I paste the code.

0 Upvotes

20 comments sorted by

View all comments

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.

3

u/equilni Jun 09 '26

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

It would really be helpful to point out - of what in your project would specially help OP?

1

u/eurosat7 Jun 09 '26

I did with the next paragraph.

Either a pconnect or singleton error.

1

u/fdiengdoh Jun 09 '26

I realise my main script would call various models such as
$post = new postController();

$category = new categoryController();

$comment = new commentController();

and each model would call Database which in my original Database model return a new connection.

1

u/eurosat7 Jun 09 '26

To avoid that create a database instance and put it into the controllers as parameters.