-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.php
166 lines (145 loc) · 3.67 KB
/
Connection.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace MySqlConnection;
use mysqli;
use mysqli_result;
class Connection
{
/**
* @var string
*/
private $charset;
/**
* @var Server
*/
private $server;
/**
* @var string
*/
private $database;
/**
* @var mysqli
*/
private $last_connection;
/**
* Connection constructor.
* @param Server $server
* @param string $database
*/
public function __construct($server, $database)
{
$this->server = $server;
$this->database = $database;
$this->charset = 'utf8';
}
/**
* new Connection class without Server object
* @param string $host
* @param string $username
* @param string $password
* @param string $dbName
* @return Connection
*/
public static function create_connection($host, $username, $password, $dbName)
{
$server = new Server($host, $username, $password);
return new Connection($server, $dbName);
}
/**
* set charset to access database
* @param $charset
*/
public function set_charset($charset)
{
$this->charset = $charset;
}
/**
* return database charset (in default is UTF8)
* @return string
*/
public function get_charset()
{
return $this->charset;
}
/**
* create new mysql connection
* @return mysqli
*/
private function get_connection()
{
$mysqli = new mysqli($this->server->getServer(), $this->server->getUsername(), $this->server->getPassword(), $this->database);
$mysqli->set_charset($this->get_charset());
return $mysqli;
}
/**
* get a string if your connection has error
* @return string
*/
public function get_connect_error()
{
return $this->get_connection()->connect_error;
}
/**
* a string description of the last error like your queries error
* @return string
*/
public function get_last_error()
{
if ($this->last_connection != null)
return mysqli_error($this->last_connection);
}
/**
* Run and get result from Mysql Query
* @param string $query
* @return bool|mysqli_result
*/
public function run_query($query)
{
if ($this->last_connection != null)
$this->last_connection->close();
$conn = $this->get_connection();
$this->last_connection = $conn;
if ($conn->connect_error) {
return false;
}
return $conn->query($query);
}
/**
* Run your query and get connection used for this run query
* @param string $query
* @param mysqli $conn
* @return bool|mysqli_result
*/
public function run_query_connection($query, &$conn)
{
$conn = $this->get_connection();
$this->last_connection = $conn;
if ($conn->connect_error) {
return false;
}
return $conn->query($query);
}
/**
* get result from mysql query have result like queries contain 'SELECT'
* @param string $query
* @return array
*/
public function run_select_query($query)
{
$selectValues = array();
$result = $this->run_query($query);
if ($result != false && $result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$select = array();
foreach (array_keys($row) as $item) {
$select[$item] = $row[$item];
}
$selectValues[] = $select;
}
}
return $selectValues;
}
public function close_last_connection()
{
$this->last_connection->close();
}
}