代码之家  ›  专栏  ›  技术社区  ›  Codecraker

无法加入并从Codeigniter中的数据库获取数据

  •  0
  • Codecraker  · 技术社区  · 7 年前

    我想加入三个表并从函数中获取数据,但无法这样做。我可以得到类别联合数据,但不能得到产品线数据。

    我有三张桌子。一个是 cv_products 第二个是 cv_category 最后一个是 product_line . 我有一张桌子 cv_产品 命名的 产品线 .

    这是我在模型中的功能

    public function get_products($id = 0, $slug = FALSE){
    if($id === 0 && $slug === FALSE){
    
    $this->db->order_by('cv_products.id', 'DESC');
    
    $this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );
    
    $this->db->join('cv_category','cv_category.id = cv_products.category', 'left');
    
    $this->db->join('product_line','product_line.id = cv_products.product_line', 'left');
    
    $query= $this->db->get('cv_products');
    
    return $query->result_array();
    
    }
    $this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );
    
    $this->db->join('cv_category','cv_category.id = cv_products.category', 'left');
    
    $this->db->join('product_line','product_line.id = cv_products.product_line', 'left');
    
    $query= $this->db->get_where('cv_products', array('cv_products.id' => $id, 'slug' => $slug));
    
    return $query->row_array();
    
    
    
    }
    

    因为它给出了一个错误,比如 Severity: Notice Message: Undefined index: linename .

    因为我有一块地 name 两者都有 cv_产品 , 简历类别 产品线 所以在选择的时候 product_line.name as linename . 没关系的 简历类别 关节,但不能得到相同的情况下 产品线 联合时间。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Simon K    7 年前

    你的选择电话里的报价好像有点离谱。

    更改此:

    $this->db->select('cv_products.*, cv_category.name as categoryname', 'product_line.name as linename','cv_category.id as category_id' );
    

    对此:

    $this->db->select('cv_products.*, cv_category.name as categoryname, product_line.name as linename,cv_category.id as category_id' );
    

    注意,我在select语句的开头打开报价单,在结尾关闭它。不需要引用单个字段 $this->db->select() 然后潜在地将它们视为函数参数。

    对函数中的第二个select语句也重复此方法。

    如果对你有用就告诉我。

    推荐文章