-
Notifications
You must be signed in to change notification settings - Fork 0
/
explore_cars.php
67 lines (66 loc) · 2.18 KB
/
explore_cars.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
<?php
function getTheQuery($qnum, $param) {
if($qnum == 1) {
$query = "SELECT model_name FROM models WHERE make_id=(SELECT make_id FROM makes WHERE make_name=\"" . $param . "\");";
}
elseif($qnum == 2) {
$query ="SELECT * FROM models WHERE horsepower >= " . $param . ";";
}
elseif($qnum == 3) {
$query = "SELECT * FROM makes WHERE makes.make_id IN (SELECT models.make_id FROM models WHERE horsepower >= " . $param. ");";
}
elseif($qnum == 4) {
$query = "SELECT * FROM makes JOIN models ON makes.make_id=models.make_id;";
}
return $query;
}
$servername = "localhost";
$username = "admin";
$password = "monarchs";
$dbname = "cars";
// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: ") . $conn->connect_error;
}
if (defined($_GET['queried'])){
$toQuery = getTheQuery($_GET['queryNumber'], $_GET['queryParameter']);
$result = $conn->query($toQuery);
if($result->num_rows > 0) {
echo "Query <i>" . $toQuery . "</i> received <b>" . $result->num_rows . "rows ...</b><br><br>";
echo "<table padding=2 border=1>\n";
$resultNum=0;
while($row=$result->fetch_assoc()) {
if($resultNum == 0) {
echo "<tr>";
foreach($row as $key => $value) {
echo "<th>" . $key;
}
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value;
}
$resultNum++;
}
echo "</table>";
}
}
mysqli_close($conn);
?>
<html>
<head>
<title>Just a quick example...</title></head>
<body>
<form method="GET">
<input type="radio" name="queryNumber" value="1"> Get all models by make... <br>
<input type="radio" name="queryNumber" value="2"> Get all makes above a specified horsepower<br>
<input type="radio" name="queryNumber" value="3"> Get all makes that have a car over a specified horsepower<br>
<input type="radio" name="queryNumber" value="4"> Get all car info from DB<br>
<input type="hidden" name="queried" value="true">
<input type="text" name="queryParameter">
<input type="submit">
</form>
</body>
</html>