-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnector.class.php
More file actions
executable file
·50 lines (43 loc) · 1.22 KB
/
Connector.class.php
File metadata and controls
executable file
·50 lines (43 loc) · 1.22 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
<?php
/**
* Lambda Creatives
* Database connection class
*/
namespace Lambda\Database;
use Mysqli;
abstract class Connector {
protected static $connection = null;
private $settings;
// Disconnect from database
public static function disconnect() {
self::$connection->close();
self::$connection = null;
}
// Check if already connected
public static function isConnected() {
if (self::getInstance()) {
$get_test = self::getInstance()->query("SELECT 1 AS test");
$connection = (boolean) $get_test->num_rows;
} else {
$connection = false;
}
return $connection;
}
public static function connect(array $connection_settings = array()) {
if ($connection_settings) {
$settings = $connection_settings;
} else {
throw new \Lambda\Database\BetterException('Failed to get settings.');
}
try {
$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['database']);
self::$connection = $mysqli;
} catch (Exception $e) {
throw new \Lambda\Database\BetterException($e->getMessage());
self::$connection = false;
}
}
public static function getInstance() {
return self::$connection;
}
}