-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqldatabase.class.php
130 lines (100 loc) · 2.67 KB
/
mysqldatabase.class.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
<?php
class MysqlDatabase
{
private $host;
private $usuario;
private $pass;
private $db;
private $connection;
function __construct($host, $usuario, $pass, $db)
{
$this->host = $host;
$this->usuario = $usuario;
$this->pass = $pass;
$this->db = $db;
}
function connect(){
$this->connection = mysqli_connect(
$this->host,
$this->usuario,
$this->pass,
$this->db
);
$this->connection->set_charset("utf8");
if (mysqli_connect_errno()) {
print("error al conectarse");
}
}
function getData($sql)
{
$data = array();
$result = mysqli_query($this->connection, $sql);
$error = mysqli_error($this->connection);
if (empty($error)) {
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
array_push($data, $row);
}
}
} else {
throw new Exception($error);
}
return $data;
}
function numRows($sql)
{
$result = mysqli_query($this->connection, $sql);
$error = mysqli_error($this->connection);
if (empty($error)) {
return mysqli_num_rows($result);
} else {
throw new Exception($error);
}
}
function getDataSingle($sql)
{
$result = mysqli_query($this->connection, $sql);
$error = mysqli_error($this->connection);
if (empty($error)) {
if (mysqli_num_rows($result) > 0) {
return mysqli_fetch_assoc($result);
}
} else {
throw new Exception($error);
}
return null;
}
function getDataSingleProp($sql, $prop)
{
$result = mysqli_query($this->connection, $sql);
$error = mysqli_error($this->connection);
if (empty($error)) {
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
return $data[$prop];
}
} else {
throw new Exception($error);
}
return null;
}
function executeInstruction($sql)
{
$success = mysqli_query($this->connection, $sql);
$error = mysqli_error($this->connection);
if (empty($error)) {
return $success;
} else {
throw new Exception($error);
}
}
function close()
{
mysqli_close($this->connection);
}
function getLastId()
{
return mysqli_insert_id($this->connection);
}
}
?>