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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动