34315-vm/db/config.php
Flatlogic Bot d627358851 v1.3
2025-09-23 16:15:21 +00:00

47 lines
1.6 KiB
PHP

<?php
// db/config.php
// --- Database Credentials ---
// Replace with your actual database credentials
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'mymecha'); // Changed from 'default_db'
define('DB_USER', 'root'); // Changed from 'user'
define('DB_PASS', ''); // Assuming empty password for local dev
// --- PDO Connection Function ---
/**
* Creates a PDO database connection.
*
* @return PDO|null A PDO connection object on success, or null on failure.
*/
function db_connect() {
static $pdo = null; // Static variable to hold the connection
if ($pdo === null) {
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
// Create the database if it doesn't exist
$temp_pdo = new PDO('mysql:host=' . DB_HOST, DB_USER, DB_PASS, $options);
$temp_pdo->exec("CREATE DATABASE IF NOT EXISTS " . DB_NAME);
// Now connect to the specific database
$pdo = new PDO($dsn, DB_USER, DB_PASS, $options);
} catch (PDOException $e) {
// In a real application, you would log this error, not display it
// For development, it's useful to see the error
error_log('Database Connection Error: ' . $e->getMessage());
// Return null or handle the error as appropriate for your application
return null;
}
}
return $pdo;
}
?>