0

I have json encode method, I use this to built an api to return json data. It's work perfectly, but after I install Xampp new version again this will display an error:

Note - This works perfectly on my previous xampp, this error came after i installed new xampp

<?php
if ($result->num_rows > 0) {

    while ($row = $result->fetch_assoc()) {

           $myObj[$a]->id   = $row["district_code"];
           $myObj[$a]->name = $row["district_name"];
           $myObj[$a]->type = 'District';

           $a++;

     }

    $myJSON = json_encode($myObj);
    echo $myJSON;
}
?>

Error:

Fatal error: Uncaught Error: Attempt to assign property "id" on null

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
madurange
  • 1
  • 1
  • Xampp probably upgraded your PHP version. You should probably check what you're installing instead of just randomly downloading all upgrades. – ADyson Aug 05 '21 at 18:01
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Nico Haase Aug 06 '21 at 14:03

1 Answers1

2

You need to create $myObj first, for example with $myObj = new stdClass();, before assigning properties to it.

Szymon
  • 88
  • 1
  • 2
  • 4