-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDb.php
106 lines (92 loc) · 2.89 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class Db {
// The database connection
protected static $connection = false;
private $username;
private $password;
private $dbname;
private $host;
public function __construct($config) {
$this->username = $config["username"];
$this->password = $config["password"];
$this->dbname = $config["dbname"];
$this->host = $config["host"];
}
/**
* Connect to the database
*
* @return bool false on failure / mysqli MySQLi object instance on success
*/
public function connect($first=true) {
// Try and connect to the database
if (self::$connection === false) {
// Load configuration as an array. Use the actual location of your configuration file
self::$connection = new mysqli($this->host, $this->username, $this->password, $this->dbname);
}
// If connection was not successful, handle the error
if (self::$connection === false) {
debug("Error connecting to database...");
// Handle error - notify administrator, log to a file, show an error screen, etc.
return false;
}
if (!self::$connection->ping()) {
debug("Connection to DB is broken...");
self::$connection->close();
self::$connection = false;
if ($first) {
debug("Trying to reconnect...");
return self::connect(false);
}
}
return self::$connection;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
public function query($query) {
// Connect to the database
$connection = $this -> connect();
// Query the database
$result = $connection -> query($query);
return $result;
}
/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
public function select($query) {
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
/**
* Fetch the last error from the database
*
* @return string Database error message
*/
public function error() {
$connection = $this -> connect();
return $connection -> error;
}
/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
public function quote($value) {
$connection = $this -> connect();
return "'" . $connection -> real_escape_string($value) . "'";
}
}