-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.php
More file actions
59 lines (46 loc) · 1.66 KB
/
main.php
File metadata and controls
59 lines (46 loc) · 1.66 KB
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
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config.php';
// Set the timezone to Kolkata
date_default_timezone_set('Asia/Kolkata');
// Load environment variables
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
try {
// Create a new database connection using the defined constants
$conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
throw new Exception("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
$conn->query("SET SESSION sql_mode = 'STRICT_ALL_TABLES'");
// Output a success message (optional)
// echo "Database connection successful!";
} catch (Exception $e) {
// Log the error message for debugging
error_log("Database connection error: " . $e->getMessage());
// Display a user-friendly error message
http_response_code(500);
die("An error occurred while connecting to the database. Please try again later.");
}
// Close the database connection when it's no longer needed
function closeDatabaseConnection($conn) {
$conn->close();
}
// Register the function to close the database connection when the script ends
register_shutdown_function(function() use ($conn) {
closeDatabaseConnection($conn);
});
// Example usage of the database connection
function getUserData($conn, $userId) {
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
$userId = 1;
$userData = getUserData($conn, $userId);
print_r($userData);
closeDatabaseConnection($conn);
?>