|
2 | 2 |
|
3 | 3 | This PDO wrapper, is a collection of methods for working with a database this includes selecting, inserting, updating and deleting records. |
4 | 4 |
|
5 | | -> Version 2+ has been rewritten for the old docs pleease see [v1 branch](https://github.com/dcblogdev/pdo-wrapper/tree/v1) |
| 5 | +> V2+ has been rewritten for the old docs please see [V1 branch](https://github.com/dcblogdev/pdo-wrapper/tree/v1) |
| 6 | +
|
| 7 | +## Upgrade from V1 |
6 | 8 |
|
7 | 9 | Version 2 is now namespaced as `Dcblogdev` instead of `Daveismyname` |
8 | 10 |
|
9 | 11 | Also the methods `get()` and `select()` have been removed. |
10 | 12 |
|
11 | | -Instead of ::get() a new instance of the class used `new Database()` |
| 13 | +Instead of ::get() a new instance of the class used `new Database($args)` |
12 | 14 |
|
13 | 15 | Select has been replaced with `->rows()` and `->row()` or `->run()` |
14 | 16 |
|
@@ -41,6 +43,69 @@ $options = [ |
41 | 43 | $db = new Database($options); |
42 | 44 | ``` |
43 | 45 |
|
| 46 | +# Quick Reference |
| 47 | +```php |
| 48 | +//create table |
| 49 | +$db->raw("CREATE TABLE demo (id int auto_increment primary key, name varchar(255))"); |
| 50 | + |
| 51 | +//use PDO directly |
| 52 | +$db->getPdo()->query('Select username FROM users')->fetchAll(); |
| 53 | + |
| 54 | +//use run to query and chain methods |
| 55 | +$db->run("SELECT * FROM users")->fetchAll(); |
| 56 | +$db->run("SELECT * FROM users")->fetch(); |
| 57 | +$db->run("SELECT * FROM users WHERE id = ?", [$id])->fetch(); |
| 58 | +//select using array instead of object |
| 59 | +$db->run("SELECT * FROM users")->fetch(PDO::FETCH_ASSOC); |
| 60 | + |
| 61 | +//get by id |
| 62 | +$db->getById('users', 2); |
| 63 | + |
| 64 | +//get all rows |
| 65 | +$db->rows("SELECT title FROM posts"); |
| 66 | +//get all rows with placeholders |
| 67 | +$db->rows("SELECT title FROM posts WHERE user_id = ?", [$user_id]); |
| 68 | + |
| 69 | +//get single row |
| 70 | +$db->row("SELECT title FROM posts"); |
| 71 | +//get single row with placeholders |
| 72 | +$db->row("SELECT title FROM posts WHERE user_id = ?", [$user_id]); |
| 73 | + |
| 74 | +//count |
| 75 | +$db->count("SELECT id FROM posts"); |
| 76 | +$db->count("SELECT id FROM posts WHERE category_id = ?", [$category_id]); |
| 77 | + |
| 78 | +//insert |
| 79 | +$id = $db->insert('users', ['username' => 'Dave', 'role' => 'Admin']); |
| 80 | + |
| 81 | +//last inserted id |
| 82 | +$db->lastInsertId()(); |
| 83 | + |
| 84 | +//update |
| 85 | +$db->update('users', ['role' => 'Editor'], ['id' => 3]); |
| 86 | + |
| 87 | +//delete from table with a where claus and a limit of 1 record |
| 88 | +$db->delete('posts', ['type_id' => 'draft'], $limit = 1); |
| 89 | + |
| 90 | +//delete from table with a where claus and a limit of 10 record |
| 91 | +$db->delete('posts', ['type_id' => 'draft'], $limit = 10); |
| 92 | + |
| 93 | +//delete all from table with a where claus and a limit of 10 record |
| 94 | +$db->delete('posts', ['type_id' => 'draft'], null); |
| 95 | + |
| 96 | +//delete all from table |
| 97 | +$db->deleteAll('posts'); |
| 98 | + |
| 99 | +//delete by id from table |
| 100 | +$db->deleteById('posts', 2); |
| 101 | + |
| 102 | +//delete by ids from table |
| 103 | +$db->deleteById('posts', '2,4,7'); |
| 104 | + |
| 105 | +//truncate table |
| 106 | +$eb->truncate('posts'); |
| 107 | +``` |
| 108 | + |
44 | 109 | # Usage examples |
45 | 110 |
|
46 | 111 | ## Accessing PDO |
|
0 commit comments