I try to fetch some data from my MySQL database by using PDO::FETCH_OBJ and json_encode but it doesn't work since I added an DATE-field in the MySQL table.
This is what my code looks like:
$app->get('/api/teams', function(Request $request, Response $response) {
$sql = "SELECT * FROM teams";
try {
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$teams= $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($teams);
} catch(PDOException $e) {
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
Before I added the DATE-field, it worked perfectly fine and it was returning an array with a bunch of objects. Now it returns no data at all. Even though, when I forexample do call /api/teams/25 - I receive the correct data, even with the date-data. So it"only" fails when I want to receive all teams/data at once.
Can someone tell me whats wrong and how to fix it?
I can also tell that I tried to do this:
foreach($teams as $team){
echo json_encode($team);
}
But this returned invalid JSON data, which was a bunch of Objects without comma separation
EDIT
My DB SCHEMA looks like this,
id (int(11), primary_key, auto_increment)
team ( varchar(255) )
country ( varchar(255) )
league ( varchar(255) )
creation_day ( date )
** EDIT 2 **
The invalid JSON which the foreach-example returns, looks like this:
{"id":"27", "Bayern München", "country":"Germany", "league": "Bundesliga, "creation_day": "2016-10-14"} {"id":"28", "Borussia Dortmund", "country":"Germany", "league": "Bundesliga, "creation_day": "2016-10-14"}
..and so on