【问题标题】:Unable to join and get the data from database in Codeigniter无法在 Codeigniter 中加入并从数据库中获取数据
【发布时间】:2018-07-27 17:30:32
【问题描述】:

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

我有三张桌子。一个是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_productscv_categoryproduct_line 中都有一个字段name,所以在选择时我已经给出了product_line.name as linenamecv_category 关节没问题,但在product_line 关节时间的情况下无法获得相同的结果。

【问题讨论】:

    标签: php codeigniter mysqli jointable


    【解决方案1】:

    您的选择调用中的引号似乎有点偏离。

    改变这个:

    $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 语句也重复此方法。

    让我知道它是否适合你。

    【讨论】:

    • 非常感谢,我花了将近 5 个小时才明白这一点,谢谢朋友
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    相关资源
    最近更新 更多