-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmydb.php
163 lines (137 loc) · 3.91 KB
/
libmydb.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
<?php
/*
** libmydb.php for libmydb in /Users/keupsonite/Documents/Sites/libmy-2014-lounes_z
**
** Made by zakaria lounes
** Login <[email protected]>
**
** Started on Sun Oct 28 13:14:07 2012 zakaria lounes
** Last update Sun Oct 28 13:14:10 2012 zakaria lounes
*/
class DBConnection
{
private $_server,
$_port,
$_user,
$_password,
$_dbname,
$_Connect,
$_SetCredentials,
$_query,
$_GestErrorMessage;
public function Connect($server, $port)
{
$this->_GestErrorMessage = NULL;
if (isset($server) && isset($port))
{
$this->_server = $server;
$this->_port = $port;
if ($this->_SetCredentials == 1)
{
$this->_Connect = mysqli_connect($server, $this->_user, $this->_password, $this->_dbname, $port);
if (!$this->_Connect)
{
$this->_GestErrorMessage = mysqli_connect_error();
return (0);
}
else
return (1);
}
else
return (0);
}
else
return (0);
}
public function SetCredentials($user, $password)
{
if (isset($user) && isset($password))
{
$this->_user = $user;
$this->_password = $password;
$this->_SetCredentials = 1;
return (1);
}
else
return (0);
}
public function Execute($query)
{
if (!$this->_Connect && $this->_SetCredentials == 1)
$this->Connect($this->_server, $this->_port);
if ($this->_Connect)
{
$this->_GestErrorMessage = NULL;
$this->_query = mysqli_query($this->_Connect, $query);
if ($this->_query)
return (1);
else
{
$this->_GestErrorMessage = mysqli_error($this->_Connect);
return (0);
}
}
else
return (0);
}
public function SelectDatabase($dbname)
{
$this->_GestErrorMessage = NULL;
if (isset($dbname) && $this->_Connect)
{
$this->_dbname = $dbname;
if (mysqli_select_db($this->_Connect, $this->_dbname))
{
mysqli_select_db($this->_Connect, $this->_dbname);
return (1);
}
else
{
$this->_GestErrorMessage = mysqli_error($this->_Connect);
return (0);
}
}
else
return (0);
}
public function GetResult()
{
$this->_GestErrorMessage = NULL;
if ($this->_query)
{
$myobject = array();
while ($object = mysqli_fetch_object($this->_query))
{
$myobject[] = $object;
}
if (count($myobject))
return ($myobject);
else
{
$this->_GestErrorMessage = mysqli_error($this->_Connect);
return (NULL);
}
}
else
return (NULL);
}
public function GestErrorMessage()
{
if ($this->_GestErrorMessage)
return ($this->_GestErrorMessage);
else
return (NULL);
}
public function Disconnect()
{
if ($this->_Connect)
{
if ($this->_query)
mysqli_free_result($this->_query);
mysqli_close($this->_Connect);
return (1);
}
else
return (0);
}
}