-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.php
More file actions
79 lines (68 loc) · 1.73 KB
/
Copy pathconfig.php
File metadata and controls
79 lines (68 loc) · 1.73 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Configuration file for database connection in an e-commerce application.
* This file defines constants for database connection parameters and
* provides functions to establish connections with and without specifying a database.
*
* @author CLÉMENT Timothé
* @version 1.0
* @since 12-01-2026
*/
/**
* Database host address
* @var string
*/
const DB_HOST = 'localhost';
/**
* Database username
* @var string
*/
const DB_USER = 'root';
/**
* Database password
* @var string
*/
const DB_PASS = 'password';
/**
* Database name
* @var string
*/
const DB_NAME = 'ecommerce_db';
/**
* Establishes a connection to the MySQL database.
*
* Creates a new MySQLi connection using the previously defined constants.
* If the connection fails, it terminates the script and outputs an error message.
*
* @return mysqli The database connection object
* @throws Exception and terminates the script with die() if connection fails
*/
function getDBConnection()
{
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
return $conn;
}
/**
* Establishes a connection to the MySQL server without specifying a database.
*
* Creates a new MySQL for development purposes where database selection is not required.
*
* @return mysqli The database connection object
* @throws Exception and terminates the script with die() if connection fails
*/
function getDBConnectionNoDB()
{
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");
return $conn;
}
?>