注意:未定义偏移量:0....override/classes/Category.php 第 18 行

问题描述

我使用 prestashop 1.6.1.12 我使用 PHP 7.1

我收到了 10 次此错误消息:

Notice: Undefined offset: 0....override/classes/Category.PHP on line 18

category.PHP 内容是:

<?PHP
/**
*
*add function for image2 for for class product.PHP
*
**/
class Category extends CategoryCore
{
    public static function getProductsImgSupp($product_id)
    {
            $sql = '
            SELECT id_image,id_product from `'._DB_PREFIX_.'image`
            WHERE id_product="'.$product_id.'"
            ORDER BY `position` ASC
            LIMIT 1,1
            ';
            $result = Db::getInstance()->ExecuteS($sql);
            return $result[0]['id_product'].'-'.$result[0]['id_image'];
    }
}

有没有办法纠正这个错误

解决方法

您的 sql 没有返回任何记录。

这样做的两个原因:

  1. ...image 中没有图像(可能是正确的)

  1. 传递给您的方法的 product_id 不正确。

如何解决?

案例 1:

$result = Db::getInstance()->ExecuteS($sql);

if (empty($result)) {
    return null; // you may have to deal with this null if you arent already
}

return $result[0]['id_product'].'-'.$result[0]['id_image'];

情况 2:

检查调用 getProductsImgSupp 的代码以查看是否传递了正确的 id。