You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -16,37 +16,44 @@ If you have successfully "installed" everything, you can use the class like that
16
16
require_once('your_path/DB.php');
17
17
```
18
18
19
-
### Instantiate Class / Connect to Database
19
+
### Instantiate Class / Connect to Database (`__construct`)
20
20
To be able to use the class and connect to the database, you have to instantiate it.
21
21
22
22
To do that, follow that format:
23
23
24
24
```php
25
-
new DB(string $dbname, string $user, string $password, string $db_type='mysql', string $host='localhost', int $pdo_err_mode=PDO::ERRMODE_EXCEPTION)
25
+
new DB(
26
+
string $dbname,
27
+
string $user,
28
+
string $password,
29
+
string $db_type='mysql',
30
+
string $host='localhost',
31
+
int $pdo_err_mode=PDO::ERRMODE_EXCEPTION
32
+
)
26
33
```
27
34
28
35
It's providing these options:
29
36
30
-
#### Database Name ('$dbname')
37
+
#### Database Name (`$dbname`)
31
38
The name of your database you want to connect with. (required)
32
39
33
-
#### Database User ('$user')
40
+
#### Database User (`$user`)
34
41
The user of the database that should interact with it. (required)
35
42
36
-
#### Password of the Database User ('$password')
43
+
#### Password of the Database User (`$password`)
37
44
The password of the database user you are using to interact with the database. (required)
38
45
39
-
#### Database Type ('$db_type')
46
+
#### Database Type (`$db_type`)
40
47
The type of the database you are using. (optional)
41
48
42
49
Default: `mysql`
43
50
44
-
#### Host ('$host')
51
+
#### Host (`$host`)
45
52
The host of your database. (optional)
46
53
47
54
Default: `localhost`
48
55
49
-
#### PDO Error Mode ('$pdo_err_mode')
56
+
#### PDO Error Mode (`$pdo_err_mode`)
50
57
The [error mode of pdo](http://php.net/manual/en/pdo.error-handling.php) you want to use. (optional)
51
58
52
59
Default: `PDO::ERRMODE_EXCEPTION`
@@ -56,7 +63,7 @@ Simple example for instantiating the class:
56
63
$db = new DB('db-class-example', 'root', '');
57
64
```
58
65
59
-
### Check Connection to Database
66
+
### Check Connection to Database (`connected()`)
60
67
The method `connected()` gives you the ability to check if the connection to the database was established successfully.
61
68
62
69
The method requires no arguments at all, so you can just call it and then it will give you a return value of `true` or `false` based on the fact if the connection was established or not.
@@ -70,7 +77,7 @@ if ($db->connected()) {
70
77
}
71
78
```
72
79
73
-
### Select/Get Data from Database
80
+
### Select/Get Data from Database (`select()`)
74
81
In order to get data from the database you can use the method `select()` in the following format:
75
82
76
83
```php
@@ -115,7 +122,134 @@ An simple example for using everything together:
An array of the values to be inserted into the database. (required)
145
+
146
+
Example:
147
+
148
+
```php
149
+
[
150
+
'username' => 'test',
151
+
'password' => 'hello'
152
+
]
153
+
```
154
+
155
+
Please note that you have to provide an associative array with keys that match to the placeholders in the sql query, unless you are not using the named placeholders in the query. In case you are just using the question marks as the placeholder, you can get rid of the keys.
156
+
157
+
Finally, here is an example for using the `insert()` method to insert data into your database:
158
+
159
+
```php
160
+
$inserted = $db->insert(
161
+
"INSERT INTO `users` (`username`, `password`) VALUES (:username, :password)",
162
+
[
163
+
'username' => 'test',
164
+
'password' => 'hello'
165
+
]
166
+
);
167
+
```
168
+
169
+
### Delete Data/Rows from Database (`delete()`)
170
+
The method `delete()` provides the ability to delete data from the database.
171
+
172
+
You have to follow this format:
173
+
174
+
```php
175
+
delete(string $sql, $where=null)
176
+
```
177
+
178
+
These are the existing arguments:
179
+
180
+
#### SQL Query (`$sql`)
181
+
The sql query to delete the data from the database. (required)
182
+
183
+
For example:
184
+
185
+
```sql
186
+
DELETEFROM`users`WHERE`id`= :id
187
+
```
188
+
189
+
#### Values for Where Clause (`$where`)
190
+
An array of the values to use in the where clause. (optional)
191
+
192
+
Default: `null`
193
+
194
+
Example:
195
+
196
+
```php
197
+
[ 'id' => 3 ]
198
+
```
199
+
200
+
Please note that you have to provide an associative array with keys that match to the placeholders in the sql query, unless you are not using the named placeholders in the query. In case you are just using the question marks as the placeholder, you can get rid of the keys.
201
+
202
+
Simple example for deleting data with this method:
203
+
204
+
```php
205
+
$deleted = $db->delete("DELETE FROM `users` WHERE `id` = :id", [ 'id' => 3 ]);
206
+
```
207
+
208
+
### Update Data in Database (`update()`)
209
+
You can update data in your database by the method `update()`, which has this format:
210
+
211
+
```php
212
+
update(string $sql, $values)
213
+
```
214
+
215
+
That leads to the following arguments:
216
+
217
+
#### SQL Query (`$sql`)
218
+
The sql query to update the data in your database. (required)
#### New Values and Values for Where Clause (`values`)
227
+
An array of the new values to which it should be updated and the values for the where clause. (required)
228
+
229
+
For example that could be the following:
230
+
231
+
```php
232
+
[
233
+
'username' => 'test',
234
+
'id' => 3
235
+
]
236
+
```
237
+
238
+
Please note that you have to provide an associative array with keys that match to the placeholders in the sql query, unless you are not using the named placeholders in the query. In case you are just using the question marks as the placeholder, you can get rid of the keys.
239
+
240
+
Example for using everything this method has to offer together:
241
+
242
+
```php
243
+
$updated = $db->update(
244
+
"UPDATE `users` SET `username` = :username WHERE `id` = :id",
An array of the error messages with the error code as the key.
135
269
136
270
Default:
@@ -175,3 +309,54 @@ DB::initErrorHandler(
175
309
'development'
176
310
);
177
311
```
312
+
313
+
### Check for Errors (`error()`)
314
+
In case you want to know if an error occured, you can simply call the method `error()` and it will return you `true` or `false` based on the fact whether there is an error or not.
315
+
316
+
Example for using that:
317
+
318
+
```php
319
+
if ($db->error()) {
320
+
echo 'There is an error!';
321
+
} else {
322
+
echo 'Everything is fine!';
323
+
}
324
+
```
325
+
326
+
### Get Errors (`getError()`)
327
+
You can get the array `$error`, which contains all errors, by calling this method.
328
+
329
+
If there are no errors, it will just return `null`.
330
+
331
+
Example for getting the whole array:
332
+
333
+
```php
334
+
$error = $db->getError();
335
+
```
336
+
337
+
Example for displaying the error message:
338
+
339
+
```php
340
+
echo $db->getError()['msg'];
341
+
```
342
+
343
+
## Further Examples / Stuff for Testing
344
+
You want to see further examples for using the database class or you just want to play around with it a little bit?
345
+
346
+
- You can find a further examples in the file [`example.php`](https://github.com/jr-cologne/db-class/blob/master/example.php).
347
+
- To play around with the database class, you can use the database provided in the file [`db-class-example.sql`](https://github.com/jr-cologne/db-class/blob/master/db-class-example.sql). Just import that in your database client and you are ready to start!
348
+
349
+
## Contributing
350
+
Feel free to contribute to this project! It would be awesome for me if somebody contributes to it.
351
+
352
+
So don't be shy and start coding! If you want to make sure that I like your idea, you can contact me by a Issue.
353
+
354
+
But if you decide to contribute to this project, keep in mind that finally it is my choice to merge your Pull Request or not, so also be prepared for a negative decision.
355
+
356
+
## Versioning
357
+
I try to follow the rules of Semantic Versioning as good as I can.
358
+
359
+
For more information about it and if you want to check out the rules, visit http://semver.org/.
360
+
361
+
## License
362
+
This project is licensed under the [MIT License](https://github.com/jr-cologne/db-class/blob/master/LICENSE).
0 commit comments