-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.php
64 lines (64 loc) · 1.51 KB
/
backend.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
<?php
function get_sample_users(){
require('config.php');
$conn_string = "mysql:host=$host;dbname=$database;charset=utf8mb4";
try{
$db = new PDO($conn_string, $username, $password);
$select_query = "select id, username from `TestUsers`";
$stmt = $db->prepare($select_query);
$r = $stmt->execute();
$response = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
}
catch(Exception $e){
$response = "DB Error: " . $e;
}
return $response;
}
if(isset($_POST["type"])){
$type = $_POST["type"];
$response = "nothing";
switch($type){
case "echo":
if(isset($_POST["message"])){
$response = "<b>Echo</b> " . $_POST["message"];
}
else{
$response = "Nothing to echo...";
}
break;
case "add":
if(isset($_POST["number1"]) && isset($_POST["number2"])){
$a = $_POST["number1"];
$b = $_POST["number2"];
if(is_numeric($a) && is_numeric($b)){
$a = (int)$a;
$b = (int)$b;
$response = ($a+$b);
}
else{
$response = "At least one of the values isn't a number";
}
}
else{
$response = "Missing one of the required fields of number1 or number2";
}
break;
case "html":
$response = "<p>I was pulled from my home backend.php</p>";
break;
case "json":
$obj->name = "Jack";
$obj->job = "Waiting at backend.php";
$obj->age = 70;
$response = json_encode($obj);
break;
case "db":
$response = get_sample_users();
break;
default:
$response = $type . " hasn't been implemented yet, please try again";
break;
}
echo $response;
}
?>