-1

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.

Qirel
  • 25,449
  • 7
  • 45
  • 62
ake
  • 1
  • 1
  • Please turn on error reporting. – waterloomatt Jul 19 '19 at 19:29
  • You've listed 2 different error msgs: DB related and also JS related. Which one are you asking about? – waterloomatt Jul 19 '19 at 19:30
  • both would be great. I'm doing my best to understand all the stuff, but it's been hard – ake Jul 19 '19 at 21:28
  • My suggestion would be to get the query going first, without AJAX. Just being able to iterate over the resultset is good enough. Once you have that working, then integrate the AJAX code. Please turn on error reporting as outlined here - https://stackoverflow.com/a/32648423/296555. This should lead you to the error. – waterloomatt Jul 22 '19 at 11:42

2 Answers2

0

If your sql statement fails $db->query(...) returns false. Check to see if your sql statement is valid and executes properly. If it is false you will get the error "Fatal error: Uncaught Error: Call to a member function fetch() on bool"

Engin
  • 76
  • 6
0

PDO::query() returns a PDOStatement object, or FALSE on failure.

  • Don't call your PHP using Ajax, at least to debug. It will be way easier to see the error. Just call your PHP route directly in a browser. json is a mess to debug in a browser console.

  • Try to catch errors and display it using PHP :

<?php
    try{
        $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);
    } catch (Exception $e) {
        var_dump($e->getMessage());
        var_dump($e->getCode());
    }
}
?>
  • Check your PHP logs (I don't know what is your OS or version of PHP but here is a quick exemple on debian, using PHP7.3-FPM :
tail -f /var/log/php7.3-fpm.log

Call your page and watch your shell.

Johnson
  • 120
  • 1
  • 10