-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDManager.php
119 lines (95 loc) · 2.34 KB
/
BDManager.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
<?php
/**
* Otherwise Studios BDManager
* @autor Juan Camilo Guarin Peñaranda
*/
class BDManager
{
private $host;
private $user;
private $pass;
private $db;
function __construct($h,$u,$p,$db)
{
$this->host=$h;
$this->user=$u;
$this->pass=$p;
$this->db=$db;
}
/*
The query function lets you make MySQL queries in a normal way
@param $query Your query
@param $encodeutf8 Wether the result has to be encoded in UTF-8 or not
*/
public function query($query, $encodeutf8=false){
$mysqli = new mysqli($this->host,$this->user,$this->pass,$this->db);
$resultado = $mysqli->query($query);
$resultados = array();
$todos=array();
while($row=$resultado->fetch_array()){
$todos[]=$row;
}
$num = $resultado->num_rows;
$fields=array();
while ($property = $resultado->fetch_field()) {
$fields[]= $property->name;
}
for($i=0; $i<$num; $i++){
$resultado2=array();
for($j=0; $j<count($fields);$j++){
if($encodeutf8){
$resultado2[$fields[$j]]=utf8_encode($todos[$i][$j]);
}else{
$resultado2[$fields[$j]]=($todos[$i][$j]);
}
}
$resultados[]=$resultado2;
}
$resultado->free();
$mysqli->close();
return $resultados;
}
/*
The insert function is just to be semantic
@param $query The query you want to make
*/
public function insert($query){
$bool = $this->update($query);
return $bool;
}
/*
The delete function is just to be semantic
@param $query The query you want to make
*/
public function delete($query){
$bool = $this->update($query);
return $bool;
}
/*
The update function makes an update to the database. Calling this function
you are able to make Inserts, deletes and updates
@param $query The query you want to make
*/
public function update($query){
$mysqli = new mysqli($this->host,$this->user,$this->pass,$this->db);
/* check connection */
if (mysqli_connect_errno()) {
printf("Error de conexión: %s\n", mysqli_connect_error());
exit();
}
$booleano = $mysqli->query($query);
/* close connection */
$mysqli->close();
return $booleano;
}
/*
The escapeString function is just to be semantic
@param $string The string you want to escape
*/
public function escapeString($string){
$mysqli = new mysqli($this->host,$this->user,$this->pass,$this->db);
$string = $mysqli->real_escape_string($string);
$mysqli->close();
return $string;
}
}