-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDb.php
executable file
·172 lines (142 loc) · 3.33 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
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
167
168
169
170
171
172
<?php
/**
* Create Database connection
*/
include_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'config.php');
Class Db {
private $_connection = null;
public function __construct() {
$this->_connect();
}
/**
* Create connection with Db
*/
private function _connect() {
$this->_connection = mysqli_init();
$_isConnected = @mysqli_real_connect(
$this->_connection,
DB_SERVER,
DB_USERNAME,
DB_PASSWORD,
DB_NAME,
DB_PORT,
null,
MYSQLI_CLIENT_FOUND_ROWS
);
if ($_isConnected === false || mysqli_connect_errno()) {
$this->closeConnection();
}
}
/**
* Check whether Db connection is exists
*/
public function isConnected() {
return ((bool) ($this->_connection instanceof mysqli));
}
/**
* Colse Db connection
*/
public function closeConnection() {
if ($this->isConnected()) {
$this->_connection->close();
}
$this->_connection = null;
}
/**
* Get last instered id
*/
public function lastInsertId() {
return $this->_connection->insert_id;
}
/**
* To execute Insert and Update queries
*
* @param string $sql query to execute
* @param array $params bind parameters required to execute query
*
* @return boolean
*/
public function execute($sql, $bind = array()) {
if ( !is_array($bind) ) {
$bind = array($bind);
}
$this->_connect();
$stmt = $this->_connection->prepare($sql);
if($stmt){
$types = "";
foreach($bind as $param) {
if (is_int($param)) $types .= "i";
else if (is_double($param)) $types .= "d";
else if (is_string($param)) $types .= "s";
}
$bind_names[] = $types;
for ($i=0; $i<count($bind);$i++)
{
$bind_name = 'bind' . $i;
$$bind_name = $bind[$i];
$bind_names[] = &$$bind_name;
}
if ( !empty( $bind ) ) {
$return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
}
$parse_query = $this->_preparedQuery($sql,$bind);
$retval = $stmt->execute();
return $retval;
}
}
/**
* To execute select queries
*
* @param string $sql query to execute
* @param array $params bind parameters required to execute query
*
* @return array
*/
public function query($sql, $bind = array()) {
if ( !is_array($bind) ) {
$bind = array($bind);
}
$this->_connect();
$stmt = $this->_connection->prepare($sql);
if($stmt){
$types = "";
foreach($bind as $param) {
if (is_int($param)) $types .= "i";
else if (is_double($param)) $types .= "d";
else if (is_string($param)) $types .= "s";
}
$bind_names[] = $types;
for ($i=0; $i<count($bind);$i++) {
$bind_name = 'bind' . $i;
$$bind_name = $bind[$i];
$bind_names[] = &$$bind_name;
}
if ( !empty( $bind ) ) {
$return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
}
$parse_query = $this->_preparedQuery($sql,$bind);
$stmt->execute();
$result = $stmt->get_result();
$returns = array();
while ($row = $result->fetch_assoc()) {
$returns[] = $row;
}
return $returns;
}
}
/**
* Prepare query to avoid SQL injection
*
* @param string $sql query to execute
* @param array $params bind parameters required to execute query
*
* @return string
*/
private function _preparedQuery($sql,$params) {
for ($i=0; $i<count($params); $i++) {
$sql = preg_replace('/\?/',$params[$i],$sql,1);
}
return $sql;
}
}
?>