'Keep getting Uncaught TypeError

User 1 is Johnny. User 2 is Billy. Billy can follow Johnny but when Johnny tries to follow Billy, I get an error. I have looked at my code over and over but can not find the issue. Error that pops up is:

Fatal error: Uncaught TypeError: array_column(): Argument #1 ($array) must be of type array, null given in C:\xampp\htdocs\Project\classes\post.classes.php:365 Stack trace: #0 C:\xampp\htdocs\Project\classes\post.classes.php(365): array_column(NULL, 'userid') #1 C:\xampp\htdocs\Project\like.php(33): Post->like_post('98168', 'user', '7500') #2 C:\xampp\htdocs\Project\index.php(23): require('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\Project\classes\post.classes.php on line 365

public function follow_user($id,$type,$mfg_userid){

    if($id == $mfg_userid && $type == 'user'){
        return;
    }

    $DB = new Database();

    $sql = "select following from likes where type='$type' && contentid = '$mfg_userid' limit 1";

    $result = $DB->read($sql);

    if(is_array($result)) {

        $likes = json_decode($result[0]['following'],true);

        $user_ids = array_column($likes, "userid");

        if(!in_array($id, $user_ids)) {

            $arr["userid"] = $id;
            $arr["date"] = date("Y-m-d H:i:s");

            $likes[] = $arr;

            $likes_string = json_encode($likes);

            $sql = "update likes set following = '$likes_string' where type='$type' && contentid = '$mfg_userid' limit 1";
            $DB->save($sql);

            $user = new User();
            $single_post = $user->get_user($id);

            add_notification($_SESSION['mfg_userid'],"follow",$single_post);

        } else {

            $key = array_search($id, $user_ids);
            unset($likes[$key]);

            $likes_string = json_encode($likes);

            $sql = "update likes set following = '$likes_string' where type='$type' && contentid = '$mfg_userid' limit 1";
            $DB->save($sql);

        }
        
    } else {

        $arr["userid"] = $id;
        $arr["date"] = date("Y-m-d H:i:s");

        $arr2[] = $arr;

        $following = json_encode($arr2);

        $sql = "insert into likes (type,contentid,following) values ('$type','$mfg_userid','$following')";
        $DB->save($sql);
        
        $user = new User();
        $single_post = $user->get_user($id);

        add_notification($_SESSION['mfg_userid'],"follow",$single_post);
    }
}
php


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source