Skip to content
Advertisement

Attempt to assign property “status” on null

I have the following code running on PHP 7.4.2, but after the update to PHP 8.1.2, I’m getting an error in order to fill an array with some data.

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    $myObj->status = '1';
    $myObj->video = $row["token"];
    $title = str_replace(".mp4","",$row["filename"]);
    $myObj->title = $title;

I’m getting the following error:

Fatal error: Uncaught Error: Attempt to assign property “status” on null

Is this problem related to some change in PHP 8? I’ve tried to find some answers but have no success on how I can update the code to run in this new scenario.

Advertisement

Answer

You need to create $myObj before you can assign to $myObj->status.

  while($row = $result->fetch_assoc()) {
    $myObj = new StdClass;
    $myObj->status = '1';
    $myObj->video = $row["token"];
    $title = str_replace(".mp4","",$row["filename"]);
    $myObj->title = $title;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement