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
Copy file name to clipboardExpand all lines: README.md
+46-50Lines changed: 46 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,49 +4,36 @@ This PDO wrapper, is a collection of crud methods for working with a database th
4
4
5
5
## Install
6
6
7
-
To install place this class into the project folder and include the class, then set the db credentials. Finally create an instance of the classes by calling it's get method.
7
+
Using composer include the repository by typing the following into a terminal
8
8
9
-
This wrapper makes use of a single database connection further connections attempts will reuse the already open connections, if not already connected.
10
-
11
-
````php
12
-
include('database.php');
9
+
```
10
+
composer require daveismyname/pdo-wrapper
11
+
```
13
12
14
-
//db properties
15
-
define('DB_TYPE','mysql');
16
-
define('DB_HOST','localhost');
17
-
define('DB_USER','username');
18
-
define('DB_PASS','password');
19
-
define('DB_NAME','database name');
13
+
Set the db credentials. Finally create an instance of the classes by calling it's get method.
20
14
21
-
// make a connection to mysql here
22
-
$db = Database::get();
23
-
````
15
+
This wrapper makes use of a single database connection further connections attempts will reuse the already open connections, if not already connected.
24
16
25
-
To make a connection to another database pass an array containing the following:
To select data 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.
46
33
47
-
````php
34
+
```php
48
35
$db->select("username FROM members WHERE memberID = :id and email = :email", array(':id' => 1, ':email' => 'someone@domain.com'));
49
-
````
36
+
```
50
37
51
38
The above query will return the username from the members table where the memberID and email match. The memberID and email is passed seperartly in an array.
52
39
@@ -56,67 +43,67 @@ Data returned from the query will be returns as an object this can be changed by
56
43
57
44
To use the object loop through it, a typical example:
58
45
59
-
````php
46
+
```php
60
47
$rows = $db->select("firstName, lastName FROM members ORDER BY firstName, lastName");
61
48
foreach ($rows as $row) {
62
49
echo "<p>$row->firstName $row->lastName</p>";
63
50
}
64
-
````
51
+
```
65
52
66
-
## Select Signle Record:
53
+
## Select Single Record:
67
54
68
55
Using find() will return only a single result. Like select it accepts params being passed in an array as a second argument.
69
56
70
-
````php
57
+
```php
71
58
$db->find("column FROM table where id=:id", [':id' => 23]);
72
-
````
59
+
```
73
60
74
61
# Raw
75
62
76
63
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.
77
64
78
-
````php
65
+
```php
79
66
$db->raw("CREATE TABLE IF NOT EXISTS members (
80
67
memberID INT(11) NOT NULL AUTO_INCREMENT,
81
68
firstName VARCHAR(255) NOT NULL,
82
69
lastnName VARCHAR(255) NOT NULL,
83
70
email VARCHAR(255) NOT NULL,
84
71
PRIMARY KEY (memberID))"
85
72
);
86
-
````
73
+
```
87
74
88
75
## Insert
89
76
90
77
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.
91
78
92
-
````php
79
+
```php
93
80
$data = array(
94
81
'firstName' => 'Joe',
95
82
'lastnName' => 'Smith',
96
83
'email' => 'someone@domain.com'
97
84
);
98
85
$db->insert('members', $data);
99
-
````
86
+
```
100
87
101
88
The insert automatically returns the last inserted id by returning 'lastInsertId' to collect the id:
102
89
103
-
````php
90
+
```php
104
91
$id = $db->insert('members', $data);
105
-
````
92
+
```
106
93
107
94
## Updating
108
95
109
96
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.
139
126
140
-
````php
127
+
```php
141
128
$where = array('memberID' => 2);
142
129
$db->delete('members', $where);
143
-
````
130
+
```
144
131
145
132
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.
146
133
147
-
````php
134
+
```php
148
135
$db->delete('members', $where, 10); //delete 10 records matcing the where
149
136
$db->delete('members', $where, null); //delete all records matching the where
150
137
```
151
138
139
+
## Delete multiple IN
140
+
141
+
To delete multiple records where ids are in a specific column, this uses WHERE id IN (4,5,6)
142
+
143
+
```php
144
+
$db->deleteByIds('users', 'id', '4,5,6');
145
+
```
146
+
152
147
## Truncate
153
148
154
149
To empty a table of all contents call the truncate method. Passing only the table name.
155
150
156
-
````php
151
+
```php
157
152
$db->truncate('members');
158
-
````
153
+
```
159
154
160
155
161
156
## Count
162
157
163
158
To count records call the count method. This method expects the table name and column name (optional).
0 commit comments