Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/sql-intro/sql-intro0/
createtable.php
deletepost.php
deletetable.php
insertdefaultposts.php
insertpost.php
selectposts.php
updatepost.php
selectposts.php
66 lines ASCII Windows (CRLF)
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
<!-- selectposts.php -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select</title>
</head>
<body>
<?php
include ('../../../dbconnection.php');
try {
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM pdodemotable";
$stmt = $dbconn->prepare($sql);
// parameters in array, if empty we could skip the $data-variable
$data = array();
$stmt->execute($data);
$res = $stmt->fetchAll();
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// fetch width column names
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_ASSOC);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// fetch columnindex
$data = array();
$stmt->execute($data);
$res = $stmt->fetch(PDO::FETCH_NUM);
$output = htmlentities(print_r($res, 1));
echo "<pre>$output</pre>";
// fetch width column names, create a table
$data = array();
$stmt->execute($data);
$output = "<table><caption>En ostylad tabell!</caption>";
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$output .= "<tr>".
"<td>".htmlentities($res['id'])."</td>".
"<td>".htmlentities($res['firstname'])."</td>".
"<td>".htmlentities($res['lastname'])."</td>".
"<td>".htmlentities($res['age'])."</td>".
"<td>".htmlentities($res['reg_date'])."</td>".
"</tr>";
}
$output .= "</table>";
echo "$output";
}
catch(PDOException $e)
{
echo $sql . "<br />" . $e->getMessage();
}
$dbconn = null;
?>
</body>
</html>