38 lines
1.1 KiB
PHP
38 lines
1.1 KiB
PHP
<?php
|
|
// db/config.php
|
|
|
|
// --- Database Credentials ---
|
|
// In a real environment, use environment variables.
|
|
define('DB_HOST', '127.0.0.1');
|
|
define('DB_NAME', 'asset_management');
|
|
define('DB_USER', 'asset_admin');
|
|
define('DB_PASS', 'password');
|
|
define('DB_CHARSET', 'utf8mb4');
|
|
|
|
/**
|
|
* Establishes a PDO database connection.
|
|
*
|
|
* @return PDO The PDO database connection object.
|
|
* @throws PDOException if the connection fails.
|
|
*/
|
|
function getDbConnection() {
|
|
static $pdo = null;
|
|
|
|
if ($pdo === null) {
|
|
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=" . DB_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, DB_USER, DB_PASS, $options);
|
|
} catch (\PDOException $e) {
|
|
// For development, it's okay to see the error.
|
|
// In production, log this and show a generic error message.
|
|
throw new \PDOException($e->getMessage(), (int)$e->getCode());
|
|
}
|
|
}
|
|
return $pdo;
|
|
}
|
|
?>
|