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:
<?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:
Fatal error: Uncaught TypeError: fwrite() expects parameter 2 to be string, bool given in
Thank you for your help!
Greetings
Sascha