Skip to content

Commit 9f6975b

Browse files
committed
update example.php
1 parent 434ad22 commit 9f6975b

File tree

2 files changed

+118
-29
lines changed

2 files changed

+118
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![GitHub license](https://img.shields.io/github/license/co0lc0der/simple-query-builder-php?style=flat-square)](https://github.com/co0lc0der/simple-query-builder-php/blob/main/LICENSE.md)
77
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/co0lc0der/simple-query-builder?color=8993be&style=flat-square)
88

9-
This is a small easy-to-use PHP component for working with a database by PDO. It provides some public methods to compose SQL queries and manipulate data. Each SQL query is prepared and safe. PDO (see `Connection` class) fetches data to _arrays_ by default. See `example/example.php` for examples. At present time the component supports MySQL and SQLite (file or memory).
9+
This is a small easy-to-use PHP component for working with a database by PDO. It provides some public methods to compose SQL queries and manipulate data. Each SQL query is prepared and safe. PDO (see `Connection` class) fetches data to _arrays_ by default. At present time the component supports MySQL and SQLite (file or memory).
1010

1111
**PAY ATTENTION! v0.2 and v0.3+ are incompatible.**
1212

example/example.php

Lines changed: 117 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,147 @@
99

1010
// ------------------------
1111

12-
$groups = $query->getAll('groups');
13-
$count = $groups->getCount();
14-
foreach ($groups->getResults() as $group) {
15-
echo $group['name'];
16-
}
12+
$results = $query->select('users')->all();
1713

1814
// ------------------------
1915

20-
$results = $query->get('users', [['id', '>', 1]])->getResults();
21-
var_dump($results);
16+
$results = $query->select('users')->where([['id', '=', 10]])->one();
17+
18+
// ------------------------
19+
20+
$results = $query->select('users')->where([
21+
['id', '>', 1],
22+
'and',
23+
['group_id', '=', 2],
24+
])->all();
25+
26+
// ------------------------
27+
28+
$results = $query->select('users')->like(['name', '%John%'])->all();
29+
// or
30+
$results = $query->select('users')->where([['name', 'LIKE', '%John%']])->all();
31+
32+
// ------------------------
33+
34+
$results = $query->select('users')->notLike(['name', '%John%'])->all();
35+
// or
36+
$results = $query->select('users')->where([['name', 'NOT LIKE', '%John%']])->all();
37+
38+
// ------------------------
39+
40+
$results = $query->select('posts')
41+
->where([['user_id', '=', 3]])
42+
->offset(14)
43+
->limit(7)
44+
->all();
45+
46+
// ------------------------
47+
48+
$results = $query->select('users', ['counter' => 'COUNT(*)'])->one();
49+
// or
50+
$results = $query->count('users')->one();
51+
2252
// ------------------------
2353

24-
$query->get('users', [
25-
['id', '>', 1],
26-
'and',
27-
['group_id', '=', 2],
28-
]);
54+
$results = $query->select(['b' => 'branches'], ['b.id', 'b.name'])
55+
->where([['b.id', '>', 1], 'and', ['b.parent_id', '=', 1]])
56+
->orderBy('b.id', 'desc')
57+
->all();
2958

3059
// ------------------------
3160

32-
$query->getFields('posts', ['id', 'category', 'title'], [['views', '>=', 1000]], 'GROUP BY `category`');
61+
$results = $query->select('posts', ['id', 'category', 'title'])
62+
->where([['views', '>=', 1000]])
63+
->groupBy('category')
64+
->all();
3365

3466
// ------------------------
3567

36-
$query->getFields('users', ['id', 'username', 'email'], [['id', '>', 2]]);
68+
$groups = $query->select('orders', ['month_num' => 'MONTH(`created_at`)', 'total' => 'SUM(`total`)'])
69+
->where([['YEAR(`created_at`)', '=', 2020]])
70+
->groupBy('month_num')
71+
->having([['total', '>', 20000]])
72+
->all();
3773

3874
// ------------------------
3975

40-
$query->getFields('users', ['count' => 'COUNT(id)']);
76+
$results = $query->select(['u' => 'users'], [
77+
'u.id',
78+
'u.email',
79+
'u.username',
80+
'perms' => 'groups.permissions'
81+
])
82+
->join('groups', ['u.group_id', 'groups.id'])
83+
->limit(5)
84+
->all();
4185

4286
// ------------------------
4387

44-
$query->join(
45-
['u' => 'users', 'groups'],
46-
['u.id', 'u.email', 'u.username', 'perms' => 'groups.permissions'],
47-
['u.group_id', '=', 'groups.id']
48-
);
88+
$results = $query->select(['cp' => 'cabs_printers'], [
89+
'cp.id',
90+
'cp.cab_id',
91+
'cab_name' => 'cb.name',
92+
'cp.printer_id',
93+
'printer_name' => 'p.name',
94+
'cartridge_type' => 'c.name',
95+
'cp.comment'
96+
])
97+
->join(['cb' => 'cabs'], ['cp.cab_id', 'cb.id'])
98+
->join(['p' => 'printer_models'], ['cp.printer_id', 'p.id'])
99+
->join(['c' => 'cartridge_types'], 'p.cartridge_id=c.id')
100+
->where([['cp.cab_id', 'in', [11, 12, 13]], 'or', ['cp.cab_id', '=', 5], 'and', ['p.id', '>', 'c.id']])
101+
->all();
102+
103+
// ------------------------
104+
105+
$new_id = $query->insert('groups', [
106+
'name' => 'Moderator',
107+
'permissions' => 'moderator'
108+
])->go();
49109

50110
// ------------------------
51111

52112
$query->insert('groups', [
53-
'name' => 'Moderator',
54-
'permissions' => 'moderator'
55-
]);
113+
['name', 'role'],
114+
['Moderator', 'moderator'],
115+
['Moderator2', 'moderator'],
116+
['User', 'user'],
117+
['User2', 'user'],
118+
])->go();
119+
120+
// ------------------------
121+
122+
$query->update('users', [
123+
'username' => 'John Doe',
124+
'status' => 'new status'
125+
])
126+
->where([['id', '=', 7]])
127+
->limit()
128+
->go();
129+
130+
// ------------------------
131+
132+
$query->update('posts', ['status' => 'published'])
133+
->where([['YEAR(`updated_at`)', '>', 2020]])
134+
->go();
135+
136+
// ------------------------
137+
138+
$query->delete('users')
139+
->where([['name', '=', 'John']])
140+
->limit()
141+
->go();
142+
143+
// ------------------------
144+
145+
$query->delete('comments')
146+
->where([['user_id', '=', 10]])
147+
->go();
56148

57149
// ------------------------
58150

59-
$query->update('users', 7, [
60-
'username' => 'John Doe',
61-
'status' => 'new status'
62-
], 'LIMIT 1');
151+
$query->truncate('users')->go();
63152

64153
// ------------------------
65154

66-
$query->delete('users', [['id', '=', 10]]);
155+
$query->drop('temporary')->go();

0 commit comments

Comments
 (0)