-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindemployee.php
executable file
·57 lines (45 loc) · 1.72 KB
/
findemployee.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Find Employee</title>
</head>
<body>
<h2>Find an Employee Record</h2>
<hr>
<?php
//access credentials file
include 'credentials.php';
//this is the php object oriented style of creating a mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);
//check for connection success
if ($conn->connect_error) {
die("MySQL Connection Failed: " . $conn->connect_error);
}
echo "MySQL Connection Succeeded<br><br>";
//pull the attribute that was passed with the html form GET request and put into a local variable.
$lastname = $_GET["lastname"];
echo "Searching for: " . $lastname;
echo "<br><br>";
//create the SQL select statement, notice the funky string concat at the end to variablize the query
//based on using the GET attribute
$sql = "SELECT first_name,last_name FROM employees where last_name = '".$lastname."'";
//put the resultset into a variable, again object oriented way of doing things here
$result = $conn->query($sql);
//if there were no records found say so, otherwise create a while loop that loops through all rows
//and echos each line to the screen. You do this by creating some crazy looking echo statements
// in the form of HTMLText . row[column] . HTMLText . row[column]. etc...
// the dot "." is PHP's string concatenator operator
if ($result->num_rows > 0){
//print rows
while($row = $result->fetch_assoc()){
echo "Employee: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
}
} else {
echo "No Records Found";
}
//always close the DB connections, don't leave 'em hanging
$conn->close();
?>
</body>
</html>