I try to access to database from PostgreSQL by using PHP. I run this code, but I got an error:
VM6722:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.success ((index):58)
at c (jquery-3.4.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.4.1.min.js:2)
at l (jquery-3.4.1.min.js:2)
at XMLHttpRequest.<anonymous> (jquery-3.4.1.min.js:2)
I'm trying to figure out but I'm very new to php. SO I would appreciate any inputs to fix this issue.
<?php
$db = new PDO('pgsql:host=localhost;dbname=webmap103;', 'postgres', 'postgres');
$sql = $db->query("SELECT id, name, image, web, category,ST_AsGeoJSON(geom, 5) as geom FROM cdmx_attractions ORDER BY name");
$features = [];
while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {
$feature = ['type'=>'Feature'];
$feature['geometry'] = json_decode($row['geom']);
unset($row['geom']);
$feature['properties'] = $row;
array_push($features, $feature);
}
$featureCollection = ['type'=>'FeatureCollection', 'features'=>$features];
echo json_encode($featureCollection);
?>
I expected to show the data on my web application. I'm using html and ajax to call php to get an access to my database on postgresql.