-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbconnect.php
More file actions
23 lines (21 loc) · 761 Bytes
/
Copy pathdbconnect.php
File metadata and controls
23 lines (21 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// Start session only once
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// Define constants only once
if (!defined('LOCALHOST')) define('LOCALHOST', 'localhost');
if (!defined('DB_USERNAME')) define('DB_USERNAME', 'root');
if (!defined('DB_PASSWORD')) define('DB_PASSWORD', '');
if (!defined('DB_NAME')) define('DB_NAME', 'library_system');
// Create/reuse a single mysqli connection
if (!isset($conn) || !($conn instanceof mysqli)) {
$conn = mysqli_connect(LOCALHOST, DB_USERNAME, DB_PASSWORD);
if (!$conn) {
die('DB connect failed: ' . mysqli_connect_error());
}
$db_select = mysqli_select_db($conn, DB_NAME);
if (!$db_select) {
die('DB select failed: ' . mysqli_error($conn));
}
}