forked from Webbprogrammering/websoft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths05-xx-PHP_PDO.html
339 lines (205 loc) · 6.23 KB
/
s05-xx-PHP_PDO.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!doctype html>
<html lang="en">
<meta charset="utf-8" />
<title>websoft</title>
<!-- Mithril HTML Slideshow styles -->
<link href="css/mithril-slideshow.css" rel="stylesheet" />
<!-- Code formatting using highlight.js -->
<link rel="stylesheet" href="css/default.css">
<link rel="stylesheet" href="css/tomorrow.css">
<script src="js/highlight.pack.js"></script>
<!-- Text formatting using Markdown through showdown.js -->
<script src="js/showdown.min.js"></script>
<script data-role="slide" data-markdown type="text/html">
# Software Development for the Web
## PHP PDO and SQL
### Mikael Roos
</script>
<script data-role="slide" data-markdown type="text/html">
# Agenda
* PHP PDO
* SQL
* Databases
* CRUD
* ORM
</script>
<script data-role="slide" data-markdown type="text/html">
#PHP PDO
* PHP Data Objects (PDO)
* Interface för att accessa olika databaser
* Kräver specifik db-driver
* Tex PDO_SQLITE
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Data access abstraction
* Data access abstraction layer
* Oavsett db - samma funktioner
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Database abstraction
* INTE database abstraction
* (skriva om SQL-satser baserat på db)
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#PDO drivers som finns
* IBM, MySQL/MariaDb, Oracle, SQL Server, mfl
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Koppla mot databas
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Data Source Name (DSN)
```
// Create a DSN for the database using its filename
$fileName = __DIR__ . "/db/jetty.sqlite";
$dsn = "sqlite:$fileName";
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Öppna databas
```
// Open the database file and catch the exception it it fails.
try {
$db = new PDO($dsn);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Failed to connect to the database using DSN:<br>$dsn<br>";
throw $e;
}
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Ställ frågan
```
// Prepare and execute the SQL statement
$stmt = $db->prepare("SELECT * FROM Jetty");
$stmt->execute();
```
* Skapa ett statment och därefter exekvera det.
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Hämta resultatet
```
// Get the results as an array with column names as array keys
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($res);
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Resultset i array
<img alt="Resultset" src="https://dbwebb.se/image/snapvt15/pdo20-connect.png" style="width: 100%">
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Olika sätt hämta resultset
* `PDO::FETCH_NUM`
* `PDO::FETCH_ASSOC`, `PDO::FETCH_BOTH`
* `PDO::FETCH_OBJ`, `PDO::FETCH_LAZY`
* `PDO::FETCH_CLASS`, `PDO::FETCH_INTO`
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#PDO klasser
* [PDO](http://php.net/manual/en/class.pdo.php)
* [PDOStatement](http://php.net/manual/en/class.pdostatement.php)
* [PDOException](http://php.net/manual/en/class.pdoexception.php)
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Prepared statements
* Kompilera SQL innan exekvering
* Kompilera en gång, exekvera många
* Bifoga parametrar
* Parametrar behöver inte "quotas"
* Skyddar SQL injection
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Prepared statements...
```
// Prepare the SQL statement
$sql = "SELECT * FROM Jetty WHERE boatType LIKE ? OR boatEngine LIKE ?";
$stmt = $db->prepare($sql);
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Prepared statements...
```
// Prepare the SQL statement
$sql = "SELECT * FROM Jetty WHERE boatType LIKE ? OR boatEngine LIKE ?";
$stmt = $db->prepare($sql);
// Execute the SQL statement using parameters to replace the ?
$params = [$search, $search];
$stmt->execute($params);
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Bra att ha
* [PDO::lastInsertId()](http://php.net/manual/en/pdo.lastinsertid.php)
* [PDO::errorInfo()](http://php.net/manual/en/pdo.errorInfo.php)
* [PDOStatement::rowCount()](http://php.net/manual/en/pdostatement.rowcount.php)
* [PDOStatement::errorInfo()](http://php.net/manual/en/pdostatement.errorinfo.php)
* [PDOStatement::debugDumpParams()](http://php.net/manual/en/pdostatement.debugdumpparams.php)
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#SQL injections
<img alt="sql injection" src="img/exploits_of_a_mom.png" style="width: 100%">
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#SQL injections...
```
search.php?search=%buster%
search.php?search=1+UNION+(SELECT+1,1,1,user,password,email+FROM+Users)--
```
```
$sql = "SELECT * FROM Jetty WHERE boatType LIKE $search OR boatEngine LIKE $search";
```
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Säkerhetshål?
* Kan du hitta säkerhetshål i exemplet?
* SQL injection?
* XSS?
* CSRF?
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Om CRUD
* Create, Read, Update, Delete
* Vanligt sätt att jobba mot databaser
* Vanlig formulärhanering
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Testkör exemplet
* [Exempel](https://dbwebb.se/repo/htmlphp/example/pdo-sqlite/)
* [Källkod](https://github.com/dbwebb-se/htmlphp/tree/master/example/pdo-sqlite)
</script>
<!-- Slide -->
<script data-role="slide" data-markdown type="text/html">
#Minns
> "Lös alltid det översta felet först och lös ett fel i taget."
</script>
<script data-role="slide" data-markdown type="text/html">
# Last words
* Questions on that?
</script>
<script data-role="slide" data-markdown type="text/html">
</script>
<!-- include essential js-script -->
<script src="js/mithril.min.js"></script>
<script src="js/mithril-slideshow.js"></script>