-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.class.php
35 lines (30 loc) · 1.38 KB
/
db.class.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
<?php
// U db_settings.php su definirani $host, $database_name, $database_username i $database_password
require_once '../../../db_settings.php';
class DB {
// Interna staticka varijabla koja cuva konekciju na bazu
private static $database = null;
// Zabranimo new DB() i kloniranje
final private function __construct() { }
final private function __clone() { }
// Statica funkcija za pristup bazi
public static function getConnection() {
// Spoji se samo ako vec nisi nekad ranije
if (DB::$database === null) {
// u globalnim varijablama su parametri za spajanje
global $host, $database_name, $database_username, $database_password;
try {
DB::$database = new PDO(
'mysql:host=' . $host . ';dbname=' . $database_name . ';charset=utf8',
$database_username, $database_password
);
DB::$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
DB::$database->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage(); exit();
}
}
return DB::$database;
}
}
?>