如何解析此元素:未定义索引

时间:2018-07-31 22:44:15

标签: php

我有此错误:如何解决? Tk

Notice: Undefined index: price_group_view in /......../Products.php on line 124

第124行是:return $this->getData()[$obj];

有元素:

    private function setData() {

      if ($this->customer->getCustomersGroupID() != 0) {
        $Qproducts = $this->db->prepare('select  p.products_id,
                                                  p.products_tax_class_id,
                                                  p.orders_view,
                                                  p.products_view,
                                                  p.products_archive,
                                                  p.products_quantity,
                                                  g.customers_group_price,
                                                  g.price_group_view,
                                                  g.orders_group_view,
                                                  g.products_group_view
                                        from :table_products p left join :table_products_groups g on p.products_id = g.products_id
                                        where p.products_status = 1
                                        and g.customers_group_id = :customers_group_id
                                        and g.products_group_view = 1
                                        and p.products_id = :products_id
                                       ');

        $Qproducts->bindInt(':products_id', $this->getID() );
        $Qproducts->bindInt(':customers_group_id', $this->customer->getCustomersGroupID() );

      } else {

        $Qproducts = $this->db->prepare('select products_id,
                                                products_tax_class_id,
                                                products_quantity,
                                                orders_view,
                                                products_view,
                                                products_archive,
                                                products_tax_class_id
                                        from :table_products
                                        where products_status = 1
                                        and products_view = 1
                                        and products_id = :products_id
                                       ');

        $Qproducts->bindInt(':products_id', $this->getID() );
      }

      $Qproducts->execute();

      return $Qproducts->fetch();
    }

    public function getData()  {
      return $this->setData();
    }

===> returns a single element of the data array
    public function get($obj)  {
      return $this->getData()[$obj];
    }

1 个答案:

答案 0 :(得分:0)

我会在

周围进行一些错误检查
  $Qproducts->execute();

  return $Qproducts->fetch();

如果查询执行后$ Qproducts-> fetch()为空,则随后的访问如“ return $ this-> getData()[$ obj];”肯定会抛出这样的错误。

您可以在调用之前测试空数组。