Project_Bank / db_connect.php
Kshitij2604's picture
Upload 30 files
102fe5c verified
<?php
/**
* Database Connection
*
* This file establishes a secure connection to the MySQL database
* and handles any connection errors.
*/
// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Database configuration
$db_config = [
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'dms'
];
// Error handling
$error_message = '';
// Create connection
try {
$conn = new mysqli(
$db_config['host'],
$db_config['username'],
$db_config['password'],
$db_config['database']
);
// Check connection
if ($conn->connect_error) {
throw new Exception("Connection failed: " . $conn->connect_error);
}
// Set charset to ensure proper encoding
$conn->set_charset("utf8mb4");
} catch (Exception $e) {
$error_message = $e->getMessage();
// Log the error
error_log("Database Connection Error: " . $error_message);
// Display user-friendly message
echo '
<div style="margin: 20px; padding: 20px; border: 1px solid #dc3545; border-radius: 5px; background-color: #f8d7da; color: #721c24;">
<h3>Database Connection Error</h3>
<p>We\'re having trouble connecting to the database. Please try again later.</p>
<p>If the problem persists, please contact the system administrator.</p>
</div>';
// Stop script execution
die();
}
?>