I'm trying to create an API with Slim to feed an app, but I came across the following problem: I can get all the db data, but I can not get a specific data.
In url: http://localhost/api/
which is where I get all the data, I can return a json with all the data registered, all right. But if I try with http://localhost/api/people/3
it returns an empty json.
This is the code I'm using:
<?php
require 'vendor/autoload.php';
require 'lib/mysql.php';
use \Slim\App;
$app = new App();
$app->get('/', 'get_peoples');
$app->get('/people/{id}', function($request, $response, $args) {
get_people_id($args['id']);
});
$app->run();
function get_peoples() {
$db = connect_db();
$sql = "SELECT * FROM peoples ORDER BY 'name'";
$exe = $db->query($sql);
$data = $exe->fetch_all(MYSQLI_ASSOC);
$db = null;
echo json_encode($data);
}
function get_people_id($people_id) {
$db = connect_db();
$sql = "SELECT * FROM peoples WHERE 'id' = '$people_id'";
$exe = $db->query($sql);
$data = $exe->fetch_all(MYSQLI_ASSOC);
$db = null;
echo json_encode($data);
}
?>