无法在 Codeigniter 中加入数据库并从中获取数据
Unable to join and get the data from database in Codeigniter
我想加入三个 table 并从函数中获取数据,但无法这样做。我可以获取品类联合数据,但不能获取产品线数据。
我有三个 table。一个是 cv_products
第二个是 cv_category
最后一个是 product_line
。我在 cv_products
中有一个名为 product_line
.
的 table 字段
这是我在模型中的函数
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
.
因为我在 cv_products
、cv_category
和 product_line
中都有一个字段 name
,所以在选择时我给出了 product_line.name as linename
。在 cv_category
联合时没问题,但在 product_line
联合时无法得到相同的。
您的 select 电话中的引号似乎有点不对。
改变这个:
$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 语句也重复此方法。
让我知道它是否适合你。
我想加入三个 table 并从函数中获取数据,但无法这样做。我可以获取品类联合数据,但不能获取产品线数据。
我有三个 table。一个是 cv_products
第二个是 cv_category
最后一个是 product_line
。我在 cv_products
中有一个名为 product_line
.
这是我在模型中的函数
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
.
因为我在 cv_products
、cv_category
和 product_line
中都有一个字段 name
,所以在选择时我给出了 product_line.name as linename
。在 cv_category
联合时没问题,但在 product_line
联合时无法得到相同的。
您的 select 电话中的引号似乎有点不对。
改变这个:
$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 语句也重复此方法。
让我知道它是否适合你。