# Database as JSON

**URL:** <https://discourse.slimframework.com/t/database-as-json/4856>\
**Category:** Questions\
**Created:** [June 18, 2021, 3:07pm UTC](https://discourse.slimframework.com/t/database-as-json/4856 "2021-06-18T15:07:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://discourse.slimframework.com/u/Sascha)\
**Post date:** [June 18, 2021, 3:07pm UTC](https://discourse.slimframework.com/t/database-as-json/4856/1 "2021-06-18T15:07:21Z")

</div>

Hello all, im new at Slim4 and i need some help. I wrote a script with Slim4 and a Database-Connection. Now i want to get the datas as json. But i didnt get it. Can someone help me? My script looks like:

```auto
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
$app = AppFactory::create();
    //Fächer auslesen
    $app->get('/ReadAPI/faecher', function (Request $request, Response $response) {
    
    $sql = "SELECT * FROM faecher";
    
    try 
    {
        //Get DB Object
        $db = new db();
        //Verbindung herstellen
        $db = $db->connect();
        
        $stmt = $db->query($sql);   
        $faecher = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
  
        $response->getBody()->write(json_encode($faecher));       
        return($response)
            ->withHeader('content-type', 'application/json')
            ->withStatus(200);
        
        
    } 
    catch (PDOException $e) 
    {
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
	
});

```

And i get an error:

```auto
Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, bool given in

```

Thank you for your help!  
Greetings  
Sascha

---

<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:** [June 18, 2021, 9:43pm UTC](https://discourse.slimframework.com/t/database-as-json/4856/2 "2021-06-18T21:43:51Z")

</div>

The `json_encode` function returns **`false`** on failure.

Change the result set to array first:

```php
$faecher = $stmt->fetchAll(PDO::FETCH_ASSOC);

```

Then try adding the `JSON_THROW_ON_ERROR` as the second parameter to see what is wrong with the database result set.

```php
$response->getBody()->write(json_encode($faecher, JSON_THROW_ON_ERROR)):

```

Then change the return statement to this:

```php
return $response
    ->withHeader('Content-Type', 'application/json')
    ->withStatus(200);

```

Optional: Change the catch block to prevent JSON syntax errors:

```php
    catch (\Exception $e) 
    {
        $response->getBody()->write(
            json_encode(
                [
                    'error' => [
                        'message' => $e->getMessage(),
                    ]
                ]
            ),
            JSON_THROW_ON_ERROR
        );

        return $response
            ->withHeader('Content-Type', 'application/json')
            ->withStatus(500);
    }

```

---

<div class="post-metadata">

**Author:** ![Sascha](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Sascha](https://discourse.slimframework.com/u/Sascha)\
**Post date:** [June 19, 2021, 4:19am UTC](https://discourse.slimframework.com/t/database-as-json/4856/3 "2021-06-19T04:19:35Z")

</div>

Thank you for your help!

Now i get this error:

```auto
{"error":{"message":null}}

```

---

<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:** [June 19, 2021, 7:32am UTC](https://discourse.slimframework.com/t/database-as-json/4856/4 "2021-06-19T07:32:12Z")

</div>

There was a typo. It should be `'message' => $e->getMessage(),`
