Skip to content

Commit 91d3a3a

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

1 file changed

Lines changed: 18 additions & 245 deletions

File tree

README.md

Lines changed: 18 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,28 @@ Instead of ::get() a new instance of the class used `new Database($args)`
1515
Select has been replaced with `->rows()` and `->row()` or `->run()`
1616

1717
## Install
18+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/dcblogdev/pdo-wrapper.svg?style=flat-square)](https://packagist.org/packages/dcblogdev/pdo-wrapper)
19+
[![Total Downloads](https://img.shields.io/packagist/dt/dcblogdev/pdo-wrapper.svg?style=flat-square)](https://packagist.org/packages/dcblogdev/pdo-wrapper)
1820

19-
Using composer include the repository by typing the following into a terminal
21+
![Logo](https://repository-images.githubusercontent.com/48907251/f6aff180-494c-11eb-8ca6-80000ee9dbf2)
2022

21-
```
22-
composer require dcblogdev/pdo-wrapper
23-
```
2423

25-
Set the db credentials. Finally create an instance of the classes.
24+
This PDO wrapper, is a collection of methods for working with a database this includes selecting, inserting, updating and deleting records.
2625

27-
```php
28-
use Dcblogdev\PdoWrapper\Database;
29-
30-
// make a connection to mysql here
31-
$options = [
32-
//required
33-
'username' => '',
34-
'database' => '',
35-
//optional
36-
'password' => '',
37-
'type' => 'mysql',
38-
'charset' => 'utf8',
39-
'host' => 'localhost',
40-
'port' => '3309'
41-
];
42-
43-
$db = new Database($options);
44-
```
26+
> V2+ has been rewritten for the old docs please see [V1 branch](https://github.com/dcblogdev/pdo-wrapper/tree/v1)
27+
28+
## Upgrade from V1
29+
30+
Version 2 is now namespaced as `Dcblogdev` instead of `Daveismyname`
31+
32+
Also the methods `get()` and `select()` have been removed.
33+
34+
Instead of ::get() a new instance of the class used `new Database($args)`
35+
36+
Select has been replaced with `->rows()` and `->row()` or `->run()`
37+
38+
# Documentation and install instructions
39+
[https://dcblog.dev/docs/pdo-wrapper](https://dcblog.dev/docs/pdo-wrapper)
4540

4641
# Quick Reference
4742
```php
@@ -105,225 +100,3 @@ $db->deleteById('posts', '2,4,7');
105100
//truncate table
106101
$eb->truncate('posts');
107102
```
108-
109-
# Usage examples
110-
111-
## Accessing PDO
112-
113-
You can call getPdo()` to get access to PDO directly:
114-
115-
```php
116-
$db->getPdo()
117-
```
118-
119-
This allows to chain calls:
120-
121-
```php
122-
$db->getPdo()->query($sql)->fetch();
123-
```
124-
125-
126-
## querying:
127-
128-
All quries use prepared statements, calling `->run()` returns a PDO option that can be chained:
129-
130-
Select multiple records:
131-
132-
```php
133-
$db->run("select * FROM users")->fetchAll();
134-
```
135-
136-
Select a single record:
137-
138-
```php
139-
$db->run("select * FROM users")->fetch();
140-
```
141-
142-
Select multiple records using `->rows`
143-
144-
```php
145-
$db->rows("select * FROM table");
146-
```
147-
148-
Select single record using `->row`
149-
150-
```php
151-
$db->row("select * FROM table");
152-
```
153-
154-
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.
155-
156-
**Names placeholders**
157-
158-
```php
159-
$db->row("select username FROM users WHERE id = :id and email = :email", ['id' => 1, ':email' => 'someone@domain.com']);
160-
```
161-
162-
**Annonomus placeholders**
163-
164-
```php
165-
$db->row("select username FROM users WHERE id = ? and email = ?", [1, 'someone@domain.com']);
166-
```
167-
168-
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.
169-
170-
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.
171-
172-
Data returned from the query will be returns as an object this can be changed by passing a third param containing PDO::FETCH_ASSOC.
173-
174-
To use the object loop through it, a typical example:
175-
176-
```php
177-
$rows = $db->rows("firstName, lastName FROM username ORDER BY firstName, lastName");
178-
foreach ($rows as $row) {
179-
echo "<p>$row->firstName $row->lastName</p>";
180-
}
181-
```
182-
183-
## Select Single Record:
184-
185-
Using row() will return only a single result. Like rows it accepts params being passed in an array as a second argument.
186-
187-
**Names placeholders**
188-
189-
```php
190-
$db->row("column FROM table where id=:id", ['id' => 23]);
191-
```
192-
193-
**Annonomus placeholders**
194-
195-
```php
196-
$db->row("column FROM table where id=?", [23]);
197-
```
198-
199-
Another way to select a signle record using the table and id by calling `->getById`
200-
201-
```php
202-
$db->getById('users', $id);
203-
```
204-
205-
# Raw
206-
207-
A raw query is a query that is not ran through a prepared statement and will execute the query passed directly. Useful when creating a table.
208-
209-
```php
210-
$db->raw("CREATE TABLE IF NOT EXISTS users (
211-
id INT(11) NOT NULL AUTO_INCREMENT,
212-
firstName VARCHAR(255) NOT NULL,
213-
lastnName VARCHAR(255) NOT NULL,
214-
email VARCHAR(255) NOT NULL,
215-
PRIMARY KEY (id))"
216-
);
217-
```
218-
219-
## Count
220-
221-
To count records call the count method. This method expects the table name and column name (optional).
222-
223-
```php
224-
$db->count('users');
225-
```
226-
227-
If table has no column `id`
228-
229-
```php
230-
$db->count('users', 'user_id');
231-
```
232-
233-
## Insert
234-
235-
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.
236-
237-
```php
238-
$data = [
239-
'firstName' => 'Joe',
240-
'lastnName' => 'Smith',
241-
'email' => 'someone@domain.com'
242-
];
243-
$db->insert('users', $data);
244-
```
245-
246-
The insert automatically returns the last inserted id by returning 'lastInsertId' to collect the id:
247-
248-
```php
249-
$id = $db->insert('users', $data);
250-
```
251-
252-
## Updating
253-
254-
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.
255-
256-
```php
257-
$data = [
258-
'firstName' => 'Joe',
259-
'lastnName' => 'Smith',
260-
'email' => 'someone@domain.com'
261-
];
262-
$where = ['id' => 2];
263-
$db->update('users', $data, $where);
264-
```
265-
Or:
266-
267-
```php
268-
$update = [
269-
'data' => [
270-
'firstName' => 'Joe',
271-
'lastnName' => 'Smith',
272-
'email' => 'someone@domain.com'
273-
],
274-
'where' => [
275-
'id' => 2
276-
]
277-
];
278-
279-
$db->update('users', $update['data'], $update['where']);
280-
281-
```
282-
283-
## Delete
284-
285-
To delete records call the delete method. This method expects the table name and an array of the where condition.
286-
287-
```php
288-
$where = ['id' => 2];
289-
$db->delete('users', $where);
290-
```
291-
292-
This will delete a single record to set the limit pass a third parameters containing the number to limit to, or to remove the limit pass null as a third param.
293-
294-
```php
295-
$db->delete('users', $where, 10); //delete 10 records matcing the where
296-
$db->delete('users', $where, null); //delete all records matching the where
297-
```
298-
299-
## Delete all
300-
301-
To delete all records for a given table
302-
303-
```php
304-
$db->deleteAll('users');
305-
```
306-
307-
## Delete by id
308-
309-
To delete a record by its table and id
310-
311-
```php
312-
$db->deleteById('users', $id);
313-
```
314-
315-
## Delete multiple IN
316-
317-
To delete multiple records where ids are in a specific column, this uses WHERE id IN (4,5,6)
318-
319-
```php
320-
$db->deleteByIds('users', 'id', '4,5,6');
321-
```
322-
323-
## Truncate
324-
325-
To empty a table of all contents call the truncate method. Passing only the table name.
326-
327-
```php
328-
$db->truncate('users');
329-
```

0 commit comments

Comments
 (0)