[SOLVED] GET request doesn't return anything

So I am using PHP slimframework for just a simple API I need to use in my React application and I cannot get my query to work with these parameters, because $start_date and $end_date are not returning anything from GET request. This MySQL works like it should, I already tested it with startDate and endDate I get back from my React app, the problem is that I cannot figure it out how to get data back from these GET requests to my $start_date and $end_date variables.

This is what my backend looks like:


<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;


$app->get('/api/date', function(Request $request, Response $response){
    $start_date=date('Y-m-d H:i:s', $_GET['startDate']);
    $end_date=date('Y-m-d H:i:s', $_GET['endDate']);
    // $start_date = $app->request()->params('startDate');
    // $end_date = $app->request()->params('endDate');
    // $start_date = $request->getAttribute['startDate'];
    // $end_date = $request->getAttribute('endDate');

    $sql = "SELECT * FROM `datescalendar` where `date` BETWEEN '{$start_date}' AND '{$end_date}'";
    // $sql = "SELECT * FROM `datescalendar` where `date` BETWEEN '1525679047' AND '1526283847'";


    try{
        // Get DB Object
        $db = new db();
        // Connect
        $db = $db->connect();

        $stmt = $db->query($sql);
        $dates = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        // return $response->withJson($dates);
        echo json_encode($dates);
    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
}); 

This is how I fetch data from API in my React app:

fetchNewDatesNext() {
    const startDate = this.state.startDate.unix();
    const endDate = this.state.startDate.add(1, 'week').unix();

    axios.get(`http://localhost/api/date?startDate=${startDate}&endDate=${endDate}`).then((response) => {
      this.setState(() => ({ data: response.data}));
    });
  };

Application is working like it should when I just query everything at once from database ($sql = "SELECT * FROM datescalendar)

Problem is definitely that $_GET just doesn’t return everything, because if If I use query with fixed startDate and endDate like this $sql = “SELECT * FROM datescalendar where date BETWEEN ‘1525679047’ AND ‘1526283847’”; then it works…
Any ideas?

I’m not sure I’m fully following, but to get the startDate and endDate from a URL like this:

http://localhost/api/date?startDate=today&endDate=tomorrow

You would typically do something like this rather than using the global:

$app->get('/api/date', function(Request $request, Response $response, $args) {
    $startDate = $request->getQueryParam('startDate');

I already tried this, but thank you anyway.

This is my situation right now:
https://gyazo.com/894d2a175f302824b74c30db46d11786
https://gyazo.com/4e45c39eaae6c93ed6e89198d600e5d4
https://gyazo.com/06e7a006f9494c03bdec134b999a7344

I am out of ideas, been stuck with this for 2 days now :confused:

Try something different:

$dates = $stmt->fetchAll(PDO::FETCH_ASSOC);

return $response->withJson($dates);

If you still get no result, make sure all the content of $dates is utf8 encoded. The reason: json_encode only accepts UTF-8 encoded values.

PS: Don’t use echo in Slim.

I already tried this as well (it is just commented out). I am pretty sure $dates is utf8 encoded, because when I change my MySQL query to "$sql = “SELECT * FROM datescalendar;” then my application is working. So every time it fetches the whole API (I cannot get query parameters working).

PDO could fail without a error message. Please make sure you enabled the PDO exception flag in your connection options.

PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

Example:

$host = '127.0.0.1';
$dbname = 'test';
$username = 'root';
$password = '';
$charset = 'utf8';
$collate = 'utf8_unicode_ci';
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";

$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_PERSISTENT => false,
    PDO::ATTR_EMULATE_PREPARES => false,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset COLLATE $collate"
];

$pdo = new PDO($dsn, $username, $password, $options);

I had it just set to “SET NAMES utf8” and changed it to “SET NAMES utf8 COLLATE utf8_unicode_ci”, but it didn’t change anything :/.
Picture: https://gyazo.com/4ecd8160877b969f89e48fd6f65372c4

Ok, then the SQL is probably not correct.

  1. The sql contains a sql injection point. Better use prepared statements.
  2. Try to run the SQL in another tool like workbench, phpmyadmin or sqlyog.
  3. Enter a fixed sql string without the $variables

I already tried my MySQL query in phpmyadmin SQL tab and in text editor with fixed sql strings I got back from my app when I click “Next Week” or “Previous Week” and it worked… I just don’t get it what could be the problem here…

// EDIT: I just found out that the problem is with my React app side logic how these parameters are changed. I am getting back valid parameters, but not for the right week. Since I am only fetching 1 week of data at the time from API I am not getting back data for the right week. Thank you for help anyway!

Also, the correct way to get get query parameters for me are:

$startDate = $request->getQueryParam(‘startDate’);
$endDate = $request->getQueryParam(‘endDate’);

please paste this in the BROWSER
with today and tomorrow
a valid date timestamp (integer)

http://localhost/api/date?startDate=today&endDate=tomorrow

then in php

var_dump($_GET);
var_dump($request->getParsedBody());

and post here results

furthemore verify that dates must integer

Thank you for help, but I found out that the problem was with my React app side logic how I get these parameters. Also to get correct parameter values I had to use:
$startDate = $request->getQueryParam(‘startDate’);
$endDate = $request->getQueryParam(‘endDate’);