如何获取rest api输出在url上输入的行数

I created a REST API and I've come to the point where I want to output 20 rows (e.g.) if I access the API like http://api.randomuser.me/?results=20.

This is my PHP code. I am new to Slim and AngularJS, please help.

function getUsers() {
    $sql = "select * FROM user";
    try {
        $db = getConnection();
        $stmt = $db->query($sql);  
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

Thanks in advance.

Read the results parameter in a variable, and use it in the LIMIT clause instead of the hard coded 10

function getUsers() {

    try {
        $db = getConnection();

        $limit = $_GET["results"];
        // validate $limit for valid value here and continue only if  
        // using something like 
        // $limit = $db->real_escape_string($limit);
        // and continue only if successfully validated

        $sql = "select * FROM user ORDER BY id LIMIT ".(strlen($limit) ? $limit : 10);
        $stmt = $db->query($sql);  
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

You will have to change your sql... something like

$limit = $_GET['limit'];
/clean your input make sure it has a value using isset,empty etc
$sql = "select * FROM user ORDER BY id LIMIT ".$limit; //this is a quick example rather use pdo bound parameters.
    try {
        $db = getConnection();
        $stmt = $db->query($sql);

        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

Remember to clean your input and use bound parameters in your query.

</div>