Migrated to new php, now get, Warning: Trying to access array offset on value of type bool in blog editor

51 Views Asked by At

I recently migrated hosts for my website which came with a newer version of php. Since the migration I have been getting the error Warning: Trying to access array offset on the value of type bool in /xxx/edit-post.php on line 150 which relates to if($row3['catID'] == $row2['catID']){ appearing between each checkbox of what is the blogpost category selection field. It does not appear to impede the function of selecting categories for the blog post.

    <legend>Categories</legend>

    <?php

    $stmt2 = $db->query('SELECT catID, catTitle FROM blog_cats ORDER BY catTitle');
    while($row2 = $stmt2->fetch()){

        $stmt3 = $db->prepare('SELECT catID FROM blog_post_cats WHERE catID = :catID AND postID = :postID') ;
        $stmt3->execute(array(':catID' => $row2['catID'], ':postID' => $row['postID']));
        $row3 = $stmt3->fetch(); 

        if($row3['catID'] == $row2['catID']){
            $checked = 'checked=checked';
        } else {
            $checked = null;
        }

        echo "<input type='checkbox' name='catID[]' value='".$row2['catID']."' $checked> ".$row2['catTitle']."<br />";
    }

    ?>

The above is the relevant section of the code from the edit-post page. I have attempted several solutions, but I truly do not understand what I am doing, the blog itself was a very simple install (even simpler than phpbb), so before I migrated I never had to contend with more than HTML, CSS, and js. What do I do to resolve this error?

1

There are 1 best solutions below

0
John Smith On

It would appear either $row2 or $row3 is being converted to a boolean (true or false) value by some mechanism in the framework elsewhere. I would recommend searching for every instance or use of $row2 or $row3 in the entire framework to find out specifically what's happening. If this is due to an update or change in PHP versions, it's reasonable to assume something else, perhaps more crucial, could be broken as well.

If you can confirm this is a lone, insignificant issue (sometimes $row2 will be an array as expected, sometimes a boolean and of no consequence elsewhere), something like this should work:

Replace if($row3['catID'] == $row2['catID']){

With if( is_array( $row3 ) && isset( $row3['catID'] && is_array( $row2 ) && isset( $row2['catID'] && $row3['catID'] == $row2['catID']){

What does this is basically check if each variable is in fact an array, then checks if each specific array key exists BEFORE the comparison.

There are a few others ways to do the same thing, such as simply checking if $row3['catID'] has been declared using isset, etc.

Again, please remember this is a cheap fix. You should discover exactly why this is happening by going through every line of code that uses and of course declares $row2 and $row3 to make sure whatever is causing this problem isn't or won't cause something dangerous that might not trigger a warning.