forked from startupjing/Tech_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabseOperation.php
107 lines (77 loc) · 1.93 KB
/
DatabseOperation.php
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
<!-- perform queries with parameters -->
<?php
require 'database.php';
$first = $_POST['first'];
$last = $_POST['last'];
$dept = $_POST['dept'];
$stmt = $mysqli->prepare("insert into employees (first_name, last_name, department) values (?, ?, ?)");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('sss', $first, $last, $dept);
$stmt->execute();
$stmt->close();
?>
<!-- perform queries that return data -->
<?php
require 'database.php';
$stmt = $mysqli->prepare("select first_name, last_name from employees order by last_name");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->execute();
$stmt->bind_result($first, $last);
echo "<ul>\n";
while($stmt->fetch()){
printf("\t<li>%s %s</li>\n",
htmlspecialchars($first),
htmlspecialchars($last)
);
}
echo "</ul>\n";
$stmt->close();
?>
<!-- combined queries -->
<?php
require 'database.php';
$dept = $_GET['dept'];
$stmt = $mysqli->prepare("select first_name, last_name from employees where department=?");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->bind_param('s', $dept);
$stmt->execute();
$stmt->bind_result($first, $last);
echo "<ul>\n";
while($stmt->fetch()){
printf("\t<li>%s %s</li>\n",
htmlspecialchars($first),
htmlspecialchars($last)
);
}
echo "</ul>\n";
$stmt->close();
?>
<!-- return results into array -->
<?php
require 'database.php';
$stmt = $mysqli->prepare("select first_name, last_name from employees order by last_name");
if(!$stmt){
printf("Query Prep Failed: %s\n", $mysqli->error);
exit;
}
$stmt->execute();
$result = $stmt->get_result();
echo "<ul>\n";
while($row = $result->fetch_assoc()){
printf("\t<li>%s %s</li>\n",
htmlspecialchars( $row["first_name"] ),
htmlspecialchars( $row["last_name"] )
);
}
echo "</ul>\n";
$stmt->close();
?>