如何修复“尝试访问类型为空值的数组偏移量错误” - PHP 7.4

问题描述

在 Xampp 上使用 PHP 7.4 处理项目时出现此错误。第 553 行是;

if ($this->data['avatar_id'] == 0)

我意识到它正在 DB_USERS 中搜索 avatar_id 但它位于 DB_ACCOUNTS 中。我认为这是导致问题的原因,但我找不到如何解决

这些是完成代码块;

    private function getData()
{
    global $config;
    $query = $this->getConnection()->query("SELECT * FROM " . DB_USERS . " WHERE id=" . $this->id);
    
    if ($query->num_rows == 1)
    {
        $fetch = $query->fetch_array(MysqLI_ASSOC);
        
        if (! empty($fetch['birthday']))
        {
            if (!preg_match('/(\-|\/)/',$fetch['birthday'])) $this->getConnection()->query("UPDATE " . DB_USERS . " SET birthday='01/01/2000' WHERE id=" . $this->id);

            $fetch['birth'] = explode('-',$fetch['birthday']);
            if (isset($fetch['birth'][1]))
            {
                $fetch['birth'] = array(
                    'date' => (int) $fetch['birth'][0],'month' => (int) $fetch['birth'][1],'year' => (int) $fetch['birth'][2]
                );
            }
            else
            {
                $fetch['birth'] = explode('/',$fetch['birthday']);
                $fetch['birth'] = array(
                    'date' => (int) $fetch['birth'][1],'month' => (int) $fetch['birth'][0],'year' => (int) $fetch['birth'][2]
                );
            }
        }
        
        if ($this->data['avatar_id'] == 0)
        {
            $fetch['thumbnail_url'] = $fetch['avatar_url'] = SITE_URL . '/' . $config['user_default_male_avatar'];
            
            if (! empty($fetch['gender']))
            {
                if ($fetch['gender'] == "female")
                {
                    $fetch['thumbnail_url'] = $fetch['avatar_url'] = SITE_URL . '/' . $config['user_default_female_avatar'];
                }
            }
        }

        $fetch['location'] = $fetch['current_city'];

        return $fetch;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)