-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-oop.php
More file actions
48 lines (39 loc) · 1.57 KB
/
database-oop.php
File metadata and controls
48 lines (39 loc) · 1.57 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
<?php
final class Database {
private static $databaseInstance = null;
private $databaseConnection = null;
private $databaseHost = "localhost";
private $databaseUser = "root";
private $databasePassword = "";
private $database = "testing_ground";
private $databasePort = 3306; //optional
private $databaseSocket = "MySQL"; //optional
private function __construct() {
$this->databaseConnection = new mysqli($this->databaseHost, $this->databaseUser, $this->databasePassword, $this->database, $this->databasePort, $this->databaseSocket);
if (mysqli_connect_error()) {
trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(), E_USER_ERROR);
exit;
}
}
private function __clone() { } // Magic method clone is empty to prevent duplication of connection
public static function getDatabaseInstance() {
if (!self::$databaseInstance) {
self::$databaseInstance = new self();
}
return self::$databaseInstance;
}
public function getDatabaseConnection() {
return $this->databaseConnection;
}
public function closeDatabaseConnection() {
$this->databaseConnection->close();
}
public function runQuery($sqlQuery) {
$this->databaseConnection->query("SET NAMES utf8");
return $this->databaseConnection->query($sqlQuery);
}
}
$db = Database::getInstance();
$mysqli = $db->getConnection();
$sql_query = "SELECT foo FROM .....";
$result = $mysqli->query($sql_query);