I'm creating a APIRest in Angular and I need to parse PHP results to JSON. I read some answer about this problem, but didn't resolve my problem.
The problem is the json_encode
return null
, and i don't know why.
$conexion = new PDO("mysql:host=localhost;dbname=blog", "root", "");
$sql = "select * from articles";
$sql = $conexion->prepare($sql);
$json = json_encode($sql->fetchAll(PDO::FETCH_ASSOC));
if ($json) {
echo $json;
} else {
echo "Error";
echo "<pre>";
print_r($json);
echo "</pre>";
}