简体   繁体   中英

Notice: Trying to access array offset on value of type bool

I get the following error when I try to open a new page from the menu. in PHP7.4

I think that mssql_num_rows() for checking if the number of rows is greater than zero will solve the problem but I can't implement it in this code due to my poor knowledge with PHP and my lack of time.

public function getCompany()
    {
        /** @var $Params array */
        $Params = $this->getURLParams();

        $sql = sprintf("
            SELECT CompanyName.ID
            FROM CompanyName, SiteTree
            WHERE CompanyName.Slug = '%s' and CompanyName.CompanyPageID = SiteTree.ID and SiteTree.URLSegment = '%s'
         ", $Params['Action'], $Params['URLSegment']);

        /** @var $row array */
        $row = DB::query($sql)->current();

        /** @var $Company CompanyName */

        $Company = DataObject::get_by_id(CompanyName::class, intval($row['ID']));

        if (!$Company instanceof CompanyName) {
            return false;
        }

        /** @var $CompanyPage CompanyPage */
        $CompanyPage = $Company->CompanyPage();

        if ($CompanyPage->getField('URLSegment') != $Params['URLSegment'])
        {
            return false;
        }

        return $Company;
    }

Any idea how to solve this problem?

Thank you.

Solved, I changed:

$Company = DataObject::get_by_id(CompanyName::class, intval($row['ID']));

to

$Company = DataObject::get_by_id(CompanyName::class, intval($row['ID']??='default value'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM