-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_emp_range_json.php
executable file
·161 lines (125 loc) · 4.6 KB
/
search_emp_range_json.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//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);
}
//pull the attribute that was passed with the html form GET request and put into a local variable.
$emp_no = $_GET["emp_no"];
//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 * FROM employees where emp_no = '".$emp_no."'";
//echo $sql; //troubleshooting statement if needed
//put the resultset into a variable
$result = $conn->query($sql);
//Iterate through the rows
if ($result->num_rows > 0){
//print rows
while($row = $result->fetch_assoc()){
#echo '{"first_name":"' . $row["first_name"]. '", "last_name": "' . $row["last_name"]. '"}';
$employee_obj=array(
"first_name" => $row["first_name"],
"last_name" => $row["last_name"]
);
echo json_encode($employee_obj);
// set response code - 200 OK
//http_response_code(200);
}
} else {
http_response_code(404);
echo "No Records Found";
}
//close db connect
$conn->close();
//always close the DB connections, don't leave 'em hanging
?>
vagrant@vagrant:~/inet4031_phpcrudecrud_lamp$ cat search_emp_no.php
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//access credentials fils
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);
}
//pull the attribute that was passed with the html form GET request and put into a local variable.
$emp_no = $_GET["emp_no"];
//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 * FROM employees where emp_no = '".$emp_no."'";
//echo $sql; //troubleshooting statement if needed
//put the resultset into a variable
$result = $conn->query($sql);
//Iterate through the rows
if ($result->num_rows > 0){
//print rows
while($row = $result->fetch_assoc()){
echo 'First Name: ' . $row["first_name"]. ';Last Name: ' . $row["last_name"];
// set response code - 200 OK
http_response_code(200);
}
} else {
http_response_code(404);
echo "No Records Found";
}
//close db connect
$conn->close();
//always close the DB connections, don't leave 'em hanging
?>
vagrant@vagrant:~/inet4031_phpcrudecrud_lamp$ cat search_emp_range_json.php
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//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);
}
//pull the attribute that was passed with the html form GET request and put into a local variable.
$emp_no_low = $_GET["emp_no_low"];
$emp_no_high = $_GET["emp_no_high"];
//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 * FROM employees where emp_no between '".$emp_no_low."' and '".$emp_no_high."'";
//echo $sql; //troubleshooting statement if needed
//put the resultset into a variable
$result = $conn->query($sql);
//Iterate through the rows
if ($result->num_rows > 0){
//print rows
//
echo "[";
while($row = $result->fetch_assoc()){
$employee_obj=array(
"employee_num" => $row["emp_no"],
"first_name" => $row["first_name"],
"last_name" => $row["last_name"],
"birth_date" => $row["birth_date"],
"hire_date" => $row["hire_date"]
);
echo json_encode($employee_obj);
echo ",";
}
echo "]";
} else {
http_response_code(404);
echo "No Records Found";
}
//close db connect
$conn->close();
//always close the DB connections, don't leave 'em hanging
?>