phppdoslimnotorm

PHP - Not entering on the loop if route returns a string


Im setting a small webservice that should return true if it finds the user, on the DB, and false if it doesn't. But its allways returning false. In fact if I echo something on the foreach it doesn't return anything, and if I echo $data I get Undefined variable: data

I've tried to search with the id, and it seems fine

If I test with, for example, a route of /api/users/login/{id}, and alter the foreach to foreach($db->user()->select('username', 'pass')->where('id', $id) as $row) it will execute correctly.

$app->get('/api/users/login/{username}/{password}', function($request){
    $username = $request -> getAttribute('username');
    $password = $request -> getAttribute('pass');
    $id = $request->getAttribute('id');
    require_once('db/dbconnect.php');

    foreach($db->user()->select('username', 'pass')->where('username', 'pass', $username, $password) as $row){
        $data[] = $row;
    }



    if(isset($data)){
        echo json_encode(true, JSON_UNESCAPED_UNICODE);
    }else{
        echo json_encode(false, JSON_UNESCAPED_UNICODE);
    }


});

Will the problem be that I'm going to fetch strings from the route? If so, how can I correct it?


Solution

  • Your usage of where() is not right, you should use it with an array of conditions.

    From the api:

    $table->where(array("field" => "x", "field2" => "y"))

    Translated to field = 'x' AND field2 = 'y' (with automatic escaping)

    $db->user()->select('username', 'pass')->where([
        'username' => $username,
        'pass' => $password
    ])