I'm building a very simple API in PHP to give me data from my DB in application/json
format.
Here is my muscular-groups.php
file :
<?php
include '../inc/db.php';
if (isset($_GET['id'])) {
$req = $pdo->prepare('SELECT * FROM muscular_groups WHERE id = ?');
$req->execute([$_GET['id']]);
$res = $req->fetchAll();
}
else {
$req = $pdo->prepare('SELECT * FROM muscular_groups');
$req->execute();
$res = $req->fetchAll();
}
header('Content-Type: application/json');
echo json_encode($res);
The problem is that in local all works well as you can see below :
But when I want to access it on my server (at www.example.com/api/muscular-groups
), I have a white screen without any error message or error logs.
What is strange : If I replace header('Content-Type: application/json');
and echo json_encode($res);
with var_dump($res)
in muscular-groups.php
, it appears on the screen.
P.S.: very strange, only one of my endpoints works on my server (eg: www.example.com/api/offers
) and displays a json output, here's the code :
<?php
include '../inc/db.php';
$req = $pdo->prepare('SELECT * FROM offers');
$req->execute();
$res = $req->fetchAll();
header('Content-Type: application/json');
echo json_encode($res);
I'm lost...