Problem getting data by ID in API with Slim

0

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);
}

?>
    
asked by anonymous 22.09.2016 / 17:06

1 answer

0

In the get_people_id function, just remove the quotes from the id column. Like this:

$sql = "SELECT * FROM peoples WHERE id = '$people_id'";

As an empty JSON comes back, it means that the syntax of parameter passing through the URL is correct, leaving the SQL query as the cause of error.

    
23.09.2016 / 03:07