File size: 1,533 Bytes
102fe5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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();
}
?>