# PDO does not return any response

**URL:** <https://discourse.slimframework.com/t/pdo-does-not-return-any-response/2418>\
**Category:** Questions\
**Created:** [May 15, 2018, 6:49pm UTC](https://discourse.slimframework.com/t/pdo-does-not-return-any-response/2418 "2018-05-15T18:49:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![serdam](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@serdam](https://discourse.slimframework.com/u/serdam)\
**Post date:** [May 15, 2018, 6:49pm UTC](https://discourse.slimframework.com/t/pdo-does-not-return-any-response/2418/1 "2018-05-15T18:49:51Z")

</div>

I’m using `Slim` framework for develop a `Restful API`. I configured the `Framework` dependencies in this way:

```
<?php

$container = $app->getContainer();

$container['db'] = function($config)
{
    $db = $config['db'];
    $pdo = new PDO('mysql:host=' . $db['host'] . ';port=' . $db['port'] . ';dbname=' . $db['dbname'], $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};

```

this file is called from `index.php` which contains the following structure:

```
<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require('vendor/autoload.php');

$settings = require __DIR__. '/src/settings.php';
$app = new \Slim\App($settings);

require __DIR__. '/src/dependencies.php';

require __DIR__. '/src/routes.php';

$app->run();

```

I read the database configuration from `settings.php`, which return an array that have the following content:

```
return [
  'settings' =>
  [    
    //Configurazione database.
    'db' =>
    [
      'port' => 3306,
      'host' => 'localhost',
      'dbname' => 'mydb',
      'user' => 'root',
      'pass' => 'root',
    ],
  ],
];

```

inside the `routes.php` I defined a simple method that return all the countries available:

```
<?php

use Slim\Http\Request;
use Slim\Http\Response;

$app->get('/get_countries', function (Request $request, Response $response, array $args)
{
    $sth = $this->db->query("SELECT * FROM country");
    $countries = $sth->fetchAll(PDO::FETCH_OBJ);
    echo json_encode($countries);
});

```

my request is done as: `http://localhost:8081/swp/get_countries`

but will return:

[![](https://i.stack.imgur.com/J9IBp.jpg) ](https://i.stack.imgur.com/J9IBp.jpg)

if I write only `echo "test"` inside `get_countries` all working well, what is wrong?

you can download my project here: [https://files.fm/u/5ww8c4be](https://files.fm/u/5ww8c4be)

---

<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 16, 2018, 9:46am UTC](https://discourse.slimframework.com/t/pdo-does-not-return-any-response/2418/2 "2018-05-16T09:46:52Z")

</div>

Hello @serdam

Just a tip: Never use `echo` in Slim. Please use and return the `$response` object instead.

Example:

```php
$statement = $this->db->query("SELECT * FROM country");
$countries = $statement->fetchAll(PDO::FETCH_ASSOC);

return $response->withJson($countries);

```
