CodeIgniter Forums
PHP error. Why? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: PHP error. Why? (/showthread.php?tid=85885)



PHP error. Why? - richb201 - 12-16-2022

This bit of code is causing a PHP error (actually a notice) at line 3460:
Trying to access array offset on value of type null.  Any idea why this is happening? 

for ($i = 0; $i < $iCount; $i++) {
            if ($row[$i]['id']==NULL)    << this is the line with the error
                continue;
            $bcs = @(implode(',', array_intersect_key($_SESSION['business_components'], array_flip(explode(',', $row[$i]['bc'])))));
            $this->db->set('business_component_txt', $bcs);
            $id=$row[$i]['id'];
            $this->db->where('id', $id);  //this is the id of the row we are updating
            $this->db->update('rental',);
        }


RE: PHP error. Why? - ikesela - 12-16-2022

maybe the array value not exist
try

if(!isset($row[$i]['id'])) continue:


RE: PHP error. Why? - Sprint - 12-20-2022

This error is likely happening because the code is trying to access an index of the array '$row' that does not exist. The code is checking for a value in the 'id' index of the array, but if there is no value, it will return NULL. This is causing the error, because the code is trying to access an array offset (the index it's trying to access) on a value of type NULL, which is not allowed. To fix this issue, you could use an isset() check to ensure that the index exists before trying to access it.