forked from kabircse/db_manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
175 lines (172 loc) · 5.08 KB
/
Database.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
173
174
175
<?php
if(!class_exists('DataBase')) {
class DataBase {
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct($host,$user,$pass,$db) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->$db = $db;
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->$db);
}
public function query($query=array())
{
$this->result = $this->mysqli->query($query);
return $this;
}
public function selectData($table=null, $select=null, $where=null, $or_where=null, $orderby=null, $limit=1, $offset=null){
if(!isset($table) && !isset($select))
return false;
$select = $this->arrayToString($select, $separator=', ',$return='values');
$this->sql = "SELECT {$select} FROM {$table}";
if($where and $or_where){
$where = $this->arrayToString($where, $separator=' AND ');
$or_where = $this->arrayToString($or_where, $separator=' OR ');
$this->sql .= " WHERE ({$where}) AND ({$or_where})";
}elseif($where and !$or_where){
$where = $this->arrayToString($where, $separator=' AND ');
$this->sql .= " WHERE {$where}";
}elseif($or_where and !$where){
$where = $this->arrayToString($or_where, $separator=' OR ');
$this->sql .= " WHERE {$where}";
}
if(isset($orderby)){
$this->sql .=" ORDER BY {$orderby}";
}
$this->sql .=" LIMIT $limit";
if(isset($offset)){
$this->sql .=" OFFSET $offset";
}
//print($this->sql."<br />");
return $this->query($this->sql);
}
public function queryData($sql = null){
if(isset($this->sql)){
return $this->query($this->sql);
}
return false;
}
public function insertData($table = null,$data=null){
if(!isset($table) || !isset($data)){
return false;
}
$this->sql = "INSERT INTO {$table}";
$columns = $this->arrayToString($data, $separator=', ',$return='keys');
$this->sql .= "($columns)";
$values = $this->arrayToString($data, $separator=', ',$return='values');
$this->sql .=" VALUES ($values)";
return $this->query($this->sql);
//return $this->insertId();
}
public function insertId(){
return $this->mysqli->insert_id;
}
public function affectedRows(){
return $this->mysqli->affected_rows;
}
public function updateData($table = null,$data=null, $where=null,$or_where=null, $limit='1'){
if(!isset($table) || !isset($data)){
return false;
}
$column = $this->arrayToString($data, $separator=', ');
$this->sql = "UPDATE {$table} SET {$column} ";
if(isset($where)){
$where = $this->arrayToString($where, $separator=' AND ');
$this->sql .= " WHERE {$where}";
}else if(isset($or_where)){
$where = $this->arrayToString($or_where, $separator=' OR ');
$this->sql .= " WHERE {$where}";
}
$this->sql .=" LIMIT $limit";
//print($this->sql);
$this->query($this->sql);
return $this->affectedRows();
}
public function deleteData($table = null, $where=null, $or_where=null){
if(!isset($table) || !isset($where)){
return false;
}
$this->sql = "DELETE FROM {$table}";
if(isset($where)){
$where = $this->arrayToString($where, $separator=' AND ');
$this->sql .= " WHERE {$where}";
}else if(isset($or_where)){
$where = $this->arrayToString($or_where, $separator=' OR ');
$this->sql .= " WHERE {$where}";
}
$this->query($this->sql);
return $this->affectedRows();
}
public function rows(){
return $this->result->fetch_object();
}
public function rowsArray(){
return $this->result->fetch_array(MYSQLI_NUM);
}
public function result(){
while($row=$this->rows()){
$result[] = $row;
}
return $result;
}
public function resultArray(){
while($row=$this->rows_array()){
$result[] = $row;
}
return $result;
}
public function numRows(){
return $this->result->num_rows;
}
public function arrayToString($data=array(), $separator='',$return='',$input=''){
if(!is_array($data)){
return $data;
}
$return = array_fill(0,count($data),$return);
$input = array_fill(0,count($data),'input');
if(is_array($data)){
$data = implode($separator, array_map(array($this,'convert_to_str'), $data, array_keys($data),$return, $input));
}
return $data;
}
public function convert_to_str ($v='', $k='', $return='', $input=''){
if(isset($input)){
$v = $this->cleanInput($v);
}
if($return==''){
return $k."='".$v."'";
}else{
if($return=='keys')
return $k;
else
return "'$v'";
}
}
public function cleanInput($input=null){
if(get_magic_quotes_gpc()) {
$input = stripslashes($input);
}
return $this->mysqli->real_escape_string(strip_tags(trim($input)));
}
public function cleanInputs($inputs=array()){
if(!is_array($inputs)){
$inputs = explode(' ', $inputs);
}
$inputs = array_map(array($this,'clean_input'), $inputs);
$inputs = implode(', ', $inputs);
}
/* free result set */
public function freeResult(){
$this->result->close();
}
//Close database connection
public function connectClose(){
return $this->mysqli->close();
}
}
}
?>