dongwen4630 2015-08-29 10:34
浏览 55

如何获取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.

  • 写回答

2条回答 默认 最新

  • dongzha5934 2015-08-29 10:43
    关注

    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() .'}}'; 
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作