Skip to content

Commit 8edb194

Browse files
authored
Update README.md
1 parent 91d3a3a commit 8edb194

1 file changed

Lines changed: 244 additions & 14 deletions

File tree

README.md

Lines changed: 244 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,6 @@ This PDO wrapper, is a collection of methods for working with a database this in
44

55
> V2+ has been rewritten for the old docs please see [V1 branch](https://github.com/dcblogdev/pdo-wrapper/tree/v1)
66
7-
## Upgrade from V1
8-
9-
Version 2 is now namespaced as `Dcblogdev` instead of `Daveismyname`
10-
11-
Also the methods `get()` and `select()` have been removed.
12-
13-
Instead of ::get() a new instance of the class used `new Database($args)`
14-
15-
Select has been replaced with `->rows()` and `->row()` or `->run()`
16-
17-
## Install
187
[![Latest Version on Packagist](https://img.shields.io/packagist/v/dcblogdev/pdo-wrapper.svg?style=flat-square)](https://packagist.org/packages/dcblogdev/pdo-wrapper)
198
[![Total Downloads](https://img.shields.io/packagist/dt/dcblogdev/pdo-wrapper.svg?style=flat-square)](https://packagist.org/packages/dcblogdev/pdo-wrapper)
209

@@ -35,9 +24,6 @@ Instead of ::get() a new instance of the class used `new Database($args)`
3524

3625
Select has been replaced with `->rows()` and `->row()` or `->run()`
3726

38-
# Documentation and install instructions
39-
[https://dcblog.dev/docs/pdo-wrapper](https://dcblog.dev/docs/pdo-wrapper)
40-
4127
# Quick Reference
4228
```php
4329
//create table
@@ -100,3 +86,247 @@ $db->deleteById('posts', '2,4,7');
10086
//truncate table
10187
$eb->truncate('posts');
10288
```
89+
90+
## Install
91+
92+
Using composer include the repository by typing the following into a terminal
93+
94+
```
95+
composer require dcblogdev/pdo-wrapper
96+
```
97+
98+
Set the DB credentials. Finally, create an instance of the classes.
99+
100+
```php
101+
use Dcblogdev\PdoWrapper\Database;
102+
103+
// make a connection to mysql here
104+
$options = [
105+
//required
106+
'username' => '',
107+
'database' => '',
108+
//optional
109+
'password' => '',
110+
'type' => 'mysql',
111+
'charset' => 'utf8',
112+
'host' => 'dev',
113+
'port' => '3309'
114+
];
115+
116+
$db = new Database($options);
117+
```
118+
119+
Accessing PDO
120+
You can call getPdo()` to get access to PDO directly:
121+
122+
```php
123+
$db->getPdo()
124+
```
125+
126+
This allows to chain calls:
127+
128+
```php
129+
$db->getPdo()->query($sql)->fetch();
130+
```
131+
132+
## Querying
133+
134+
All queries use prepared statements, calling ->run() returns a PDO option that can be chained:
135+
136+
Select multiple records:
137+
138+
```php
139+
$db->run("select * FROM users")->fetchAll();
140+
```
141+
142+
Select a single record:
143+
144+
```php
145+
$db->run("select * FROM users")->fetch();
146+
```
147+
148+
Select multiple records using ->rows
149+
150+
```php
151+
$db->rows("select * FROM table");
152+
```
153+
154+
Select single record using ->row
155+
156+
```php
157+
$db->row("select * FROM table");
158+
```
159+
160+
To select records based on user data instead of passing the data to the query directly use a prepared statement, this is safer and stops any attempt at sql injections.
161+
162+
Names placeholders
163+
164+
```php
165+
$db->row("select username FROM users WHERE id = :id and email = :email", ['id' => 1, ':email' => 'someone@domain.com']);
166+
```
167+
168+
Annonomus placeholders
169+
170+
```php
171+
$db->row("select username FROM users WHERE id = ? and email = ?", [1, 'someone@domain.com']);
172+
```
173+
174+
The above query will return the username from a users table where the id and email match. The id and email is passed seperartly in an array.
175+
176+
Instead of passing in an id and email to the query directly a placeholder is used :id and :email (or ? can be used) then an array is passed the keys in the array matches the placeholder and is bound, so the database will get both the query and the bound data.
177+
178+
Data returned from the query will be returns as an object this can be changed by passing a third param containing PDO::FETCH_ASSOC.
179+
180+
To use the object loop through it, a typical example:
181+
182+
```php
183+
$rows = $db->rows("firstName, lastName FROM username ORDER BY firstName, lastName");
184+
foreach ($rows as $row) {
185+
echo "<p>$row->firstName $row->lastName</p>";
186+
}
187+
```
188+
189+
## Select Single Record
190+
191+
Using row() will return only a single result. Like rows it accepts params being passed in an array as a second argument.
192+
193+
Names placeholders
194+
195+
```php
196+
$db->row("column FROM table where id=:id", ['id' => 23]);
197+
```
198+
199+
Anonymous placeholders
200+
201+
```php
202+
$db->row("column FROM table where id=?", [23]);
203+
```
204+
205+
Another way to select a single record using the table and id by calling ->getById
206+
207+
```php
208+
$db->getById('users', $id);
209+
```
210+
211+
## Raw
212+
A raw query is a query that does not run through a prepared statement and will execute the query passed directly. Useful when creating a table.
213+
214+
```php
215+
$db->raw("CREATE TABLE IF NOT EXISTS users (
216+
id INT(11) NOT NULL AUTO_INCREMENT,
217+
firstName VARCHAR(255) NOT NULL,
218+
lastnName VARCHAR(255) NOT NULL,
219+
email VARCHAR(255) NOT NULL,
220+
PRIMARY KEY (id))"
221+
);
222+
```
223+
224+
## Count
225+
To count records call the count method. This method expects the table name and column name (optional).
226+
227+
```php
228+
$db->count('users');
229+
```
230+
231+
If the table has no column id
232+
233+
```php
234+
$db->count('users', 'user_id');
235+
```
236+
237+
## Insert
238+
239+
Data is inserted by calling the insert method it expects the table name followed by an array of key and values to insert in to the database.
240+
241+
```php
242+
$data = [
243+
'firstName' => 'Joe',
244+
'lastnName' => 'Smith',
245+
'email' => 'someone@domain.com'
246+
];
247+
$db->insert('users', $data);
248+
```
249+
250+
The insert automatically returns the last inserted id by returning 'lastInsertId' to collect the id:
251+
252+
```php
253+
$id = $db->insert('users', $data);
254+
```
255+
256+
## Updating
257+
To update an existing record the update method is called. This method expects the table, array of data to update, and a second array containing the where condition.
258+
259+
```php
260+
$data = [
261+
'firstName' => 'Joe',
262+
'lastnName' => 'Smith',
263+
'email' => 'someone@domain.com'
264+
];
265+
$where = ['id' => 2];
266+
$db->update('users', $data, $where);
267+
```
268+
269+
Or:
270+
271+
```php
272+
$update = [
273+
'data' => [
274+
'firstName' => 'Joe',
275+
'lastnName' => 'Smith',
276+
'email' => 'someone@domain.com'
277+
],
278+
'where' => [
279+
'id' => 2
280+
]
281+
];
282+
283+
$db->update('users', $update['data'], $update['where']);
284+
```
285+
286+
## Delete
287+
288+
To delete records call the delete method. This method expects the table name and an array of the where condition.
289+
290+
```php
291+
$where = ['id' => 2];
292+
$db->delete('users', $where);
293+
```
294+
295+
This will delete a single record to set the limit pass a third parameter containing the number to limit to or to remove the limit pass null as a third param.
296+
297+
```php
298+
$db->delete('users', $where, 10); //delete 10 records matcing the where
299+
$db->delete('users', $where, null); //delete all records matching the where
300+
```
301+
302+
## Delete All
303+
304+
To delete all records for a given table
305+
306+
```php
307+
$db->deleteAll('users');
308+
```
309+
310+
## Delete by Id
311+
312+
To delete a record by its table and id
313+
314+
```php
315+
$db->deleteById('users', $id);
316+
```
317+
318+
## Delete Multiple In
319+
320+
To delete multiple records where ids are in a specific column, this uses WHERE id IN (4,5,6)
321+
322+
```php
323+
$db->deleteByIds('users', 'id', '4,5,6');
324+
```
325+
326+
## Truncate
327+
328+
To empty a table of all contents call the truncate method. Passing only the table name.
329+
330+
```php
331+
$db->truncate('users');
332+
```

0 commit comments

Comments
 (0)