Skip to content

Commit 6c048b1

Browse files
committed
quick reference
1 parent e59d10d commit 6c048b1

1 file changed

Lines changed: 67 additions & 2 deletions

File tree

README.md

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
This PDO wrapper, is a collection of methods for working with a database this includes selecting, inserting, updating and deleting records.
44

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
68

79
Version 2 is now namespaced as `Dcblogdev` instead of `Daveismyname`
810

911
Also the methods `get()` and `select()` have been removed.
1012

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)`
1214

1315
Select has been replaced with `->rows()` and `->row()` or `->run()`
1416

@@ -41,6 +43,69 @@ $options = [
4143
$db = new Database($options);
4244
```
4345

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+
44109
# Usage examples
45110

46111
## Accessing PDO

0 commit comments

Comments
 (0)