-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.php
66 lines (54 loc) · 1.96 KB
/
db.php
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
<?php
/*
<!--
Database was created based on these configurations:
CREATE DATABASE rabittrial;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE advertisements (
id INT AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
title VARCHAR(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id)
);
-------------------------------- TEST --------------------------------
'users' table:
INSERT INTO users (name) VALUES ('Test Elek');
'advertisement' table:
INSERT INTO advertisements (userid, title) VALUES (1, 'smthng');
----------------------------------------------------------------------
-->*/
//import
require_once 'Error/ErrorHandler.php';
try {
// Set path to .env file
$envFilePath = __DIR__ . '/.env';
// Check if the .env file exists
if (file_exists($envFilePath)) {
// read .env file line by line
$lines = file($envFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$envVars = [];
// Read variables stored in .env file
foreach ($lines as $line) {
list($key, $value) = explode('=', $line, 2);
$envVars[$key] = $value;
}
// Check that the required keys are really defined
if (isset($envVars['DB_HOST'], $envVars['DB_USERNAME'], $envVars['DB_PASSWORD'], $envVars['DB_NAME'])) {
$dbHost = $envVars['DB_HOST'];
$dbUsername = $envVars['DB_USERNAME'];
$dbPassword = $envVars['DB_PASSWORD'];
$dbName = $envVars['DB_NAME'];
// Creating connection, with host, username, password and dbname
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
}
}
} catch (Exception $e) {
//Log the exception is there any
ErrorHandler::logError("Database Error", "A database error occurred", $e);
}
// Optionally set charset
$conn->set_charset("utf8");
?>