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

2
  • Xampp probably upgraded your PHP version. You should probably check what you're installing instead of just randomly downloading all upgrades. Commented Aug 5, 2021 at 18:01
  • Does this answer your question? Reference - What does this error mean in PHP? Commented Aug 6, 2021 at 14:03

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

$myObj = new stdClass(); doesn't work with array like this $myObj[$a]->type = 'District';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.