-
Notifications
You must be signed in to change notification settings - Fork 1
/
04_select_from_table.html
73 lines (44 loc) · 1.31 KB
/
04_select_from_table.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
<html>
<head><title>mysql: Select from a table</title></head>
<body>
<h1>Without bind variables</h1>
<?php
$db_conn = new mysqli('localhost', 'rene', 'renes_secret');
if ($db_conn -> connect_errno) {
echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
return;
}
$db_conn -> select_db("renes_db");
$stmt = $db_conn->query("select id, txt from tq84_table");
print "<table>\n";
while ($row = $stmt->fetch_array()) {
printf (" <tr><td>%d</td><td>%s</td></tr>\n", $row['id'], $row['txt']);
}
print "</table>";
$stmt -> close();
$db_conn -> close();
?>
<h1>With bind variables</h1>
<?php
$db_conn = new mysqli('localhost', 'rene', 'renes_secret');
if ($db_conn -> connect_errno) {
echo "Failed to connect to MySQL: " . $db_conn -> connect_error;
return;
}
$db_conn -> select_db("renes_db");
$stmt = $db_conn->prepare("select id, txt from tq84_table where id > ?");
$two = 2;
$stmt -> bind_param('i', $two);
$stmt -> execute();
$result = $stmt -> get_result();
print "<table>\n";
while ($row = $result->fetch_array()) {
printf (" <tr><td>%d</td><td>%s</td></tr>\n", $row['id'], $row['txt']);
}
print "</table>";
$stmt -> close();
$db_conn -> close();
?>
<p>Now, <a href='05_cleanup.html'>clean the thing up</a>
</body>
</html>