使用PHP和MySQL的FILTER RESTful API

时间:2018-07-24 07:45:31

标签: sql api

我正尝试在MySQL的宁静API中创建多重过滤器。这是我的代码:

$app->get('/api/search/{last_name}{first_name}', function(Request $request, Response $response){
$first_name = $request->getAttribute('first_name');
$last_name = $request->getAttribute('last_name');

$sql = "SELECT * FROM customers WHERE last_name LIKE '%$last_name%' OR first_name LIKE '%first_name%'";

try{
    //get DB OBJECT
    $db = new db();
    //connect
    $db = $db->connect();

    $stmt = $db->query($sql);
    $customer = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;

    echo json_encode($customer);
}catch(PDOException $e){
    echo '{"error": {"text": '.$e->getMessage().'}';

}
});

用于过滤多个字段并使用我在URL中输入的关键字获取所有数据的正确代码是什么?

0 个答案:

没有答案
相关问题