You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -15,33 +15,28 @@ Instead of ::get() a new instance of the class used `new Database($args)`
15
15
Select has been replaced with `->rows()` and `->row()` or `->run()`
16
16
17
17
## Install
18
+
[](https://packagist.org/packages/dcblogdev/pdo-wrapper)
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.
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.
0 commit comments