forked from marcokuang/cs174hw3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryDB.php
More file actions
71 lines (60 loc) · 1.58 KB
/
queryDB.php
File metadata and controls
71 lines (60 loc) · 1.58 KB
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
<!DOCTYPE html>
<body>
<h1>Automobile DB Query Result</h1>
<p>
<?php
$manu = filter_input(INPUT_GET, "Manufacturer");
$model = filter_input(INPUT_GET, "Model");
$location = filter_input(INPUT_GET, "Location");
if (strlen(trim($manu)) == 0 ) {
$manu = "No input";
}
if (strlen(trim($model)) == 0 ) {
$model = "No input";
}
try{
// Connect to the database
$con = new PDO("mysql:host=localhost;dbname=Developers", "Developers", "marco1991");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM Automobile";
print "<table border='1'>\n";
//fetch the database field names.
$result = $con->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
//construct the header row of the HTML table
print "
<tr>
\n";
foreach ($row as $field => $value) {
print "
<th>$field</th>\n";
}
print "
<tr>\n";
// Constrain the query if we got manufacturer, model and location info
if ((strlen($manu) > 0) && (strlen($model) >0) && (strlen($location) >0) ) {
$query = "SELECT * FROM Automobile WHERE Manufacturer = '$manu' AND Model = '$model' AND Location = '$location'";
}
//Fetch the matching database table rows
$data = $con->query($query);
$data -> setFetchMode(PDO::FETCH_ASSOC);
// constructed the HTML table row by row
foreach ($data as $row) {
print "<tr>
\n";
foreach ($row as $name => $value) {
print"
<td>$value</td>\n";
}
print "
</tr>\n";
}
print"</table>\n";
}
catch(PDOException $ex){
echo 'ERROR:'.$ex->getMessage();
}
?>
</p>
</body>
</html>