-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConnection.class.php
89 lines (76 loc) · 1.56 KB
/
Connection.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
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
<?php
/**
* Description of Connection
*
* @author Lucas
*/
class Connection extends mysqli {
/**
*
* @var bool
*/
private static $_connected = false;
/**
*
* @var Connection
*/
private static $_instance = null;
/**
*
*/
public function __destruct() {
$this->close();
}
/**
*
* @return Connection
*/
public static function getInstance() {
if(self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
*
*/
public function connect() {
if(!self::$_connected) {
parent::__construct(
"localhost",
"user",
"pass",
"database"
);
if(mysqli_connect_errno()) {
throw new Exception("Connection failed: ".mysqli_connect_error());
}
self::$_connected = true;
}
}
/**
*
*/
public function close() {
if(self::$_connected) {
parent::close();
self::$_connected = false;
}
}
/**
*
* @param string $sql
* @return mysqli_result
*/
public function query($sql) {
$this->connect();
$result = parent::query(mysqli::escape_string($sql));
if($result) {
return $result;
}
else {
throw new Exception('Query Exception: '.mysqli_error($this).' num:'.mysqli_errno($this));
}
}
}
?>