1

i am trying to parse GeoJson from database with php but somehow i got an error and i cannot found any error in my script. here my script

$id = 1;
$db = new mysqli("localhost","root","","skriboo");
$sql = $db->query('SELECT * FROM profil_sekolah WHERE id_sekolah='.$id);
$sql2 = $db->query('SELECT * FROM pegawai WHERE id_pegawai='.$id);
$sql3 = $db->query('SELECT * FROM kelas_x A, kelas_xi B, kelas_xii C WHERE A.id_x=B.id_xi AND B.id_xi=C.id_xii AND C.id_xii='.$id);
$sql4 = $db->query('SELECT * FROM fasilitas WHERE id_sekolah='.$id);
$sql5 = $db->query('SELECT * FROM galeri where id_sekolah='.$id);
$sql6 = $db->query('SELECT bujur, lintang FROM lokasi WHERE id_lokasi='.$id);
$geojson = array(
'type' => 'FeatureCollection',
'features' => array(
    'type' => 'feature',
    'properties' => array(),
    'geometry' => array()
));

while ($row = $sql->fetch_array(MYSQLI_ASSOC)) {
    $properties = $row;
    array_push($geojson['properties'], $properties);
}

while ($row2 = $sql2->fetch_array(MYSQLI_ASSOC)) {
    $properties2 = $row2;
    array_push($geojson['properties'], $properties2);
}

while ($row3 = $sql3->fetch_array(MYSQLI_ASSOC)) {
    $properties3 = $row3;
    array_push($geojson['properties'], $properties3);
}

while ($row4 = $sql4->fetch_array(MYSQLI_ASSOC)) {
    $properties4 = $row4;
    array_push($geojson['properties'], $properties4);
}

while ($row5 = $sql5->fetch_array(MYSQLI_ASSOC)) {
    $properties5 = $row5;
    array_push($geojson['properties'], $properties5);
}

while ($row6 = $sql6->fetch_array(MYSQLI_ASSOC)) {
    $geom = array(
        'type' => 'point',
        'coordinates' => array(
            $row6['bujur'],
            $row6['lintang']
        )
    );
    array_push($geojson['geometry'], $geom);
}

header('content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
$db = null;

and then i check it in browser and i got this

array_push() expects parameter 1 to be array, null given in

maybe there is something wrong with my script and i hope there is an answer to solve this problem

  • 1
    not very experienced in PHP, but if I'm not mistaken, you skip one nested level of your $geojson object; try sth. like array_push($geojson['features']['properties'], $properties) (and the same with the geometry array) maybe!? – ThingumaBob Apr 30 '18 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.