-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.MarySQL.php
More file actions
235 lines (207 loc) · 5.49 KB
/
Copy pathclass.MarySQL.php
File metadata and controls
235 lines (207 loc) · 5.49 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* MarySQL class
* Developed by Omar BERRAYTI
* <berrayti.omar@gmail.com>
* You can use this class for personal or commercial project.
* You can edit on the class but you are not allowed to remove the author copyrights.
**/
class MarySQL{
// Connection to the DB
public $Hostname = MYSQL_HOST; // MySQL Hostname
public $Username = MYSQL_USER; // MySQL Username
public $Password = MYSQL_PASS; // MySQL Password
public $Database = MYSQL_NAME; // MySQL Database
// Debuging
public $debug = true;
// Check if there is a connection
protected $isConnected = false;
// PDO connection
protected $pdo;
/**
* The class constructor
**/
public function __construct(){
$this->Connect();
}
/**
* Responsable of showing the errors
* @param string $msg the message to show if the $debug is set to true
* @param string $msg2 the message to show if the $debug is set to false
**/
protected function log($msg,$msg2=null){
if($this->debug == true){
die($msg);
}else{
if(isset($msg2))
die($msg2);
else
die("An error occurred.");
}
}
/**
* Connect to the DB
**/
public function Connect(){
if ($this->isConnected) return;
try {
$this->pdo = new PDO('mysql:host='.$this->Hostname.';dbname='.$this->Database, $this->Username, $this->Password);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->isConnected = true;
} catch (PDOException $e) {
$this->log("<strong>Error !</strong> ".$e->getMessage(),"Can not connect to the database");
}
}
/**
* Bind params
* @param PDOStatement $s PDO Statement
* @param array $values the values to bind
* @return void
**/
protected function bindParams($s,$values){
foreach ($values as $k => &$v) {
if(is_integer($k)){
if(is_null($v)){
$s->bindValue($k+1,null,PDO::PARAM_NULL);
}else{
$s->bindParam($k+1,$v,PDO::PARAM_INT);
}
}else{
if(is_null($v)){
$s->bindValue($k,null,PDO::PARAM_NULL);
}else{
$s->bindParam($k,$v,PDO::PARAM_STR);
}
}
}
}
/**
* Execute a SQL query
* @param string $sql the sql query to run
* @param array $values an array containing the values you want to bind
* @param string $fetch the type of result you want (object,array....)
* @return the type of data you want (assoc,object,default)
**/
public function execute($sql,$values=array(),$fetch='assoc'){
$fetches = array(
'assoc' => PDO::FETCH_ASSOC,
'default' => PDO::FETCH_BOTH,
'object' => PDO::FETCH_OBJ
);
try {
$query = $this->pdo->prepare($sql);
$this->bindParams($query,$values);
$query->execute();
return $query->fetchAll($fetches[$fetch]);
} catch (PDOException $e) {
$this->log("<strong>Error !</strong> ".$e->getMessage());
}
}
/**
* Find some data from the DB
* @param array $query the query conditions
**/
public function find($query,$values=array(),$fetch='assoc'){
$sql = "SELECT ";
// Fields
if(isset($query['fields'])){
if(is_array($query['fields'])){
$sql .= implode(',', $query['fields']).' ';
}else{
$sql .= $query['fields'];
}
}else{
$sql .= "* ";
}
// Table
$sql .= " FROM ".$query['table']." as ".$query['table']." ";
// Joins
if(isset($query['join'])){
$joins = array(
'full' => 'FULL',
'inner' => 'INNER',
'right' => 'RIGHT',
'left' => 'LEFT'
);
$join = $joins[$query['join'][0]];
foreach ($query['join'][1] as $table => $fields) {
$sql .= "$join JOIN $table as $table ON $fields ";
}
}
// Conditions
if(isset($query['conditions'])){
$sql .= "WHERE ";
if(!is_array($query['conditions'])){
$sql .= $query['conditions'];
}else{
$sql .= implode(' AND ', $this->secure($query['conditions']));
}
}
// Order
if(isset($query['order'])){
$sql .= " ORDER BY ".$query['order'];
}
// Limit
if(isset($query['limit'])){
$sql .= " LIMIT ".$query['limit'];
}
return $this->execute($sql,$values,$fetch);
}
/**
* Find one result
* @param array $query the query conditions
**/
public function findFirst($query,$values=array(),$fetch='assoc'){
return current($this->find($query,$values,$fetch));
}
/**
* Insert or update data
* @param array $data an array containing the data you want to save
* @return true if everything is righ, or an error if there is any errors while saving the data
**/
public function Save($data){
$fields = array();
foreach ($data['data'] as $k => $v) {
$fields[] = "$k=:$k";
}
if(isset($data['id']) && !empty($data['id'])){
$sql = "UPDATE ".$data['table']." SET ".implode(',',$fields)." WHERE ".$data['id'][0]."=".$data['id'][1];
}else{
$sql = "INSERT INTO ".$data['table']." SET ".implode(',',$fields);
}
$query = $this->pdo->prepare($sql);
$this->bindParams($query,$data['data']);
$query->execute();
return true;
}
/**
* Delete data
* @param string $table the table name
* @param array $conditions the conditions of the row to delete
* @param array $values if you want to pass variables to the query
**/
public function delete($table,$conditions,$values=array()){
$sql = "DELETE FROM $table WHERE ";
if(!is_array($conditions)){
$sql .= $conditions;
}else{
$sql .= implode(' AND ', $this->secure($conditions));
}
//die($sql);
$query = $this->pdo->prepare($sql);
$this->bindParams($query,$values);
$query->execute();
}
/**
* Secure data
**/
protected function secure($data){
$result = array();
foreach ($data as $k => $v) {
$v = (strpos($v, ':') === 0) ? $v : '"'.mysql_escape_string($v).'"';
$result[] = "$k=$v";
}
return $result;
}
}
?>