dongqing5575 2018-07-31 22:44
浏览 69
已采纳

如何解决这个元素:未定义的索引[重复]

I have this error : how to resolve ? Tk

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

line 124 is : return $this->getData()[$obj];

there the elements :

    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];
    }
</div>
  • 写回答

1条回答 默认 最新

  • dtu11716 2018-07-31 22:54
    关注

    I would do some error checking around

      $Qproducts->execute();
    
      return $Qproducts->fetch();
    

    If $Qproducts->fetch() is empty after the query is executed, then a subsequent access like "return $this->getData()[$obj];" will surely throw such an error.

    You can test for an empty array prior to the call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么