45 lines
1.3 KiB
PHP
45 lines
1.3 KiB
PHP
<?php
|
|
|
|
// IMPORTANT: Do not output any HTML or echo statements in this file.
|
|
// This file is intended for configuration and database connection logic.
|
|
|
|
/**
|
|
* Establishes a database connection using PDO.
|
|
*
|
|
* @return PDO The PDO database connection object.
|
|
*/
|
|
function db(): PDO
|
|
{
|
|
static $pdo = null;
|
|
|
|
if ($pdo) {
|
|
return $pdo;
|
|
}
|
|
|
|
// Database credentials from environment variables
|
|
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
|
$port = getenv('DB_PORT') ?: '3306';
|
|
// Temporary fix: Hardcoded credentials to solve an environment issue where Apache cannot access the correct variables.
|
|
$dbname = 'app_36806';
|
|
$user = 'app_36806';
|
|
$pass = '72fbec7a-4020-4357-9e44-f0aad5499af6';
|
|
$charset = 'utf8mb4';
|
|
|
|
$dsn = "mysql:host=$host;port=$port;dbname=$dbname;charset=$charset";
|
|
|
|
$options = [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
PDO::ATTR_EMULATE_PREPARES => false,
|
|
];
|
|
|
|
try {
|
|
$pdo = new PDO($dsn, $user, $pass, $options);
|
|
} catch (PDOException $e) {
|
|
// In a real application, you would log this error and show a generic error page.
|
|
// For development, it's okay to die and show the error.
|
|
throw new PDOException($e->getMessage(), (int)$e->getCode());
|
|
}
|
|
|
|
return $pdo;
|
|
} |