Skip to content

Commit 4683efe

Browse files
authored
Merge pull request #10 from jr-cologne/v2.0.1
Version 2.0.1
2 parents 7f67446 + 9ff664a commit 4683efe

File tree

7 files changed

+968
-24
lines changed

7 files changed

+968
-24
lines changed

README.md

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ composer require jr-cologne/db-class
2424
Then you just have to include the autoloader:
2525

2626
```php
27-
require_once('vendor/autoload.php');
27+
require_once 'vendor/autoload.php';
2828
```
2929

3030
### Manual Installation
@@ -34,9 +34,9 @@ require_once('vendor/autoload.php');
3434
3. Include all files of the database class into your project like that:
3535

3636
```php
37-
require_once('path/to/db-class/src/DB.php');
38-
require_once('path/to/db-class/src/QueryBuilder.php');
39-
require_once('path/to/db-class/src/Exceptions/UnsupportedKeywordException.php');
37+
require_once 'path/to/db-class/src/DB.php';
38+
require_once 'path/to/db-class/src/QueryBuilder.php';
39+
require_once 'path/to/db-class/src/Exceptions/UnsupportedKeywordException.php';
4040
```
4141

4242
Now you should be ready to start!
@@ -52,7 +52,7 @@ use JRCologne\Utils\Database\DB;
5252
use JRCologne\Utils\Database\QueryBuilder;
5353
```
5454

55-
### Instantiate Class (`DB::__construct()`)
55+
### Instantiating Class
5656

5757
To be able to use the class, you have to instantiate it.
5858

@@ -62,11 +62,127 @@ Just do this:
6262
$db = new DB(new QueryBuilder);
6363
```
6464

65-
### Where can I find the documentation?
65+
### Connecting to Database
66+
67+
You can connect to a database with the help of the method `DB::connect()`.
68+
69+
An simple example:
70+
71+
```php
72+
if ($db->connect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'root', 'root')) {
73+
echo 'Successfully connected to database';
74+
} else {
75+
echo 'Connection failed';
76+
}
77+
```
78+
79+
### Retrieving Data from Database
80+
81+
In order to retrieve data from a database, you need to walk through the following three steps:
82+
83+
1. Choose a table with the method `DB::table()`.
84+
2. Select the data you want to retrieve.
85+
3. Retrieve the selected data.
86+
87+
Fortunately, this is super simple with the database class:
88+
89+
```php
90+
$data = $db->table('users')->select('*')->retrieve();
91+
92+
if ($data === false) {
93+
echo 'Ops, something went wrong retrieving the data from the database!<br>';
94+
} else if (empty($data)) {
95+
echo 'It looks like there is no data in the database!<br>';
96+
} else {
97+
echo 'Successfully retrieved the data from the database!<br>';
98+
99+
echo '<pre>', print_r($data, true), '</pre>';
100+
}
101+
```
102+
103+
It will basically retrieve all records from the selected table.
104+
105+
### Inserting Data into Database
106+
107+
If you want to insert data into a database, you have two methods which you can use:
108+
109+
- `DB::insert()` (to insert one row of data)
110+
- `DB::multi_insert()` (to insert multiple rows of data)
111+
112+
In this case, we are just going to insert one row.
113+
114+
The procedure is as follows:
115+
116+
1. Choose a table with the method `DB::table()`.
117+
2. Insert the data with the method `DB::insert()`.
118+
119+
Example:
120+
121+
```php
122+
$inserted = $db->table('users')->insert('username, password', [
123+
'username' => 'test',
124+
'password' => 'password'
125+
]);
126+
127+
if ($inserted) {
128+
echo 'Data has successfully been inserted';
129+
} else if ($inserted === 0) {
130+
echo 'Ops, some data could not be inserted';
131+
} else {
132+
echo 'Inserting of data is failed';
133+
}
134+
```
135+
136+
### Updating Data of Database
137+
138+
In case you want to update data of a database, you can use the method `DB::update()`.
139+
140+
The following steps are required:
141+
142+
1. Choose a table with the method `DB::table()`.
143+
2. Update the data with the method `DB::update()`.
144+
145+
Example:
146+
147+
```php
148+
if (
149+
$db->table('users')->update(
150+
[
151+
'username' => 'test123', // new data
152+
'password' => 'password123',
153+
],
154+
[
155+
'username' => 'test', // where clause
156+
'password' => 'password',
157+
]
158+
)
159+
) {
160+
echo 'Data has successfully been updated';
161+
} else {
162+
echo 'Updating data failed';
163+
}
164+
```
165+
166+
This will update the record(s) where the `username` is equal to `test` and the `password` is equal to `password` to `test123` for the `username` and `password123` for the `password`.
167+
168+
### Deleting Data from Database
66169

67-
There is currently no real documentation. But don't worry, it's coming soon!
170+
In order to delete data from a database, follow these steps:
68171

69-
Until then, you can simply take a look at the code and you will probably understand most things as I commented every property and method of the database class.
172+
1. Choose a table with the method `DB::table()`.
173+
2. Delete the data with the method `DB::delete()`.
174+
175+
Here's an simple example which deletes the record(s) where the `username` is equal to `test`:
176+
177+
```php
178+
if ($db->table('users')->delete([
179+
'username' => 'test' // where clause
180+
])) {
181+
echo 'Data has successfully been deleted';
182+
} else {
183+
echo 'Deleting data failed';
184+
}
185+
```
70186

71187
### Using PDO's functionality
72188

@@ -100,9 +216,17 @@ $stmt->execute();
100216
$results = $stmt->fetchAll();
101217
```
102218

219+
### API
220+
221+
Looking for a complete overview of each property and method of this database class?
222+
223+
Just head over to the [`API.md`](https://github.com/jr-cologne/db-class/blob/master/src/API.md) file where you can find everything you need.
224+
225+
It is located in the source (`src`) folder.
226+
103227
## Further Examples / Stuff for Testing
104228

105-
You want to see further examples for using the database class or you just want to play around with it a little bit?
229+
You want to see further examples of using the database class or you just want to play around with it a little bit?
106230

107231
- You can find further examples in the file [`example/example.php`](https://github.com/jr-cologne/db-class/blob/master/example/example.php).
108232
- To play around with the database class, you can use the database provided in the file [`example/db-class-example.sql`](https://github.com/jr-cologne/db-class/blob/master/example/db-class-example.sql). Just import it in your database client and you are ready to start!

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "jr-cologne/db-class",
33
"type": "library",
4-
"version": "2.0.0",
5-
"description": "A simple database class with php and pdo.",
4+
"version": "2.0.1",
5+
"description": "A simple database class with PHP, PDO and a query builder.",
66
"keywords": [
77
"database",
88
"db-class",
99
"php",
10-
"pdo"
10+
"pdo",
11+
"query-builder"
1112
],
1213
"require": {
1314
"php": ">=7.0"
@@ -18,7 +19,7 @@
1819
}
1920
},
2021
"homepage": "https://github.com/jr-cologne/db-class",
21-
"time": "2017-10-03",
22+
"time": "2017-10-21",
2223
"license": "MIT",
2324
"authors": [
2425
{
@@ -30,7 +31,7 @@
3031
],
3132
"support": {
3233
"issues": "https://github.com/jr-cologne/db-class/issues",
33-
"source": "https://github.com/jr-cologne/db-class/tree/v2.0.0",
34+
"source": "https://github.com/jr-cologne/db-class/tree/v2.0.1",
3435
"docs": "https://github.com/jr-cologne/db-class/blob/README.md"
3536
}
3637
}

example/example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require_once('../vendor/autoload.php');
3+
require_once '../vendor/autoload.php';
44

55
use JRCologne\Utils\Database\DB;
66
use JRCologne\Utils\Database\QueryBuilder;

0 commit comments

Comments
 (0)