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

135
March 27, 2019, at 6:10 PM

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?

Answer 1

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
])
Rent Charter Buses Company
READ ALSO
Openssl decryption ( DES ) returns false in PHP

Openssl decryption ( DES ) returns false in PHP

I'm trying to decrypt data with DES-ECB encryption, but the response is always false

201
How to include <?php echo esc_html($product->get_review_count()); ?> in function.php?

How to include <?php echo esc_html($product->get_review_count()); ?> in function.php?

I'd like to add :<?php echo esc_html($product->get_review_count()); ?> in my function which is :

152
Can i set password for already generated pdf file using mpdf or any other php pdf libraries

Can i set password for already generated pdf file using mpdf or any other php pdf libraries

I need to set password for my already generated pdf file using mpdf in php

155
How to change last modified date for file on my Minio storage using Laravel S3 driver?

How to change last modified date for file on my Minio storage using Laravel S3 driver?

I am storing uploaded files on my Minio storage using Laravel S3 driverProblem is that all files have a current timestamp as their "Last modified" date

334