# \[SOLVED\] GET request doesn't return anything

**URL:** <https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398>\
**Category:** Questions\
**Created:** [May 8, 2018, 11:56am UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398 "2018-05-08T11:56:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 8, 2018, 11:56am UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/1 "2018-05-08T11:56:00Z")

</div>

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

<?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:

```php
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?

---

<div class="post-metadata">

**Author:** ![tflight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tflight/32/1205_2.png) [@tflight](https://discourse.slimframework.com/u/tflight)\
**Post date:** [May 8, 2018, 12:10pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/2 "2018-05-08T12:10:00Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 8, 2018, 7:27pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/3 "2018-05-08T19:27:01Z")

</div>

I already tried this, but thank you anyway.

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

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

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 8, 2018, 8:08pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/4 "2018-05-08T20:08:22Z")

</div>

Try something different:

```php
$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.

---

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 8, 2018, 8:29pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/5 "2018-05-08T20:29:21Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 8, 2018, 8:39pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/6 "2018-05-08T20:39:13Z")

</div>

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

```php
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

```

Example:

```php
$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);

```

---

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 8, 2018, 9:16pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/7 "2018-05-08T21:16:14Z")

</div>

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](https://gyazo.com/4ecd8160877b969f89e48fd6f65372c4)

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 8, 2018, 9:24pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/8 "2018-05-08T21:24:36Z")

</div>

> [@Kristjan](#):
>
> $sql = “SELECT \* FROM `datescalendar` where `date` BETWEEN ‘{$start\_date}’ AND ‘{$end\_date}’”;

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

---

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 8, 2018, 9:33pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/9 "2018-05-08T21:33:37Z")

</div>

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’);

---

<div class="post-metadata">

**Author:** ![aghezzi](https://avatars.discourse-cdn.com/v4/letter/a/7993a0/32.png) [@aghezzi](https://discourse.slimframework.com/u/aghezzi)\
**Post date:** [May 10, 2018, 8:53pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/10 "2018-05-10T20:53:06Z")

</div>

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

[http://localhost/api/date?startDate=today&endDate=tomorrow](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

---

<div class="post-metadata">

**Author:** ![Kristjan](https://avatars.discourse-cdn.com/v4/letter/k/41988e/32.png) [@Kristjan](https://discourse.slimframework.com/u/Kristjan)\
**Post date:** [May 10, 2018, 9:15pm UTC](https://discourse.slimframework.com/t/solved-get-request-doesnt-return-anything/2398/11 "2018-05-10T21:15:16Z")

</div>

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’);
