I am trying to create a RESTful web service and having problem with the url part. I this index.php?ID=2 that displays e.g
{
"name": "Raymond",
"Nation": "USA"
}
But I want to convert the 'index.php?ID=2' to e.g 'localhost/club/player/2/' and still have the same output.
{
"name": "Raymond",
"Nation": "USA"
}
Also lets say I have only 3 rows in my database with ID 1, 2, 3. How do I adjust my code so that when someone used correct ID, Ok 200 message displays or if ID is outside the one in my database error 400 would display.
Here is my code:
$dbhost = '127.0.0.1'; //host connection
$dbuser = "root"; //root connet
$dbpass = ""; //password intialization
$db = 'adminlogin'; //Database specification
$conn = new mysqli($dbhost, $dbuser, $dbpass, $db);
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
$id = $_GET['ID'];
if ($id){
$id = mysqli_real_escape_string($conn, $id);
} else {
echo 'no ID';
}
$query = "SELECT Name, Position, Nationality, Market FROM staffdb WHERE ID ='" . $id . "'";
$result = mysqli_query($conn, $query);
if (!$result) {
printf("Error: %s
", mysqli_error($conn));
exit();
}
$temp = array();
if ($result) {
while ($row = mysqli_fetch_object($result)) {
$temp = $row;
}
header('Content-Type: application/json; charset=UTF-8');
$json_string = json_encode($temp, JSON_PRETTY_PRINT);
print $json_string;
} else {
echo 'no array';
}
Thanks alot!