Codeigniter 查询生成器加入 3 table

Codeigniter query builder to join 3 table

我需要你的帮助,我已经 table 像这样

我想在单击带 id_product 的按钮视图时查看详细产品。 这就是我所做的.. 看来我需要 id_product 之间的单引号 .. 或者是否有来自 codeigniter 的查询构建器来实现这个?

function get_detail_product($id_product){
        $query = "SELECT 
        p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty
        FROM tbl_product as p, tbl_category as c, tbl_brand as b 
        WHERE p.id_Category = c.id_Category and p.id_brand = b.id_brand
        and id_product = ".$id_product;
        return $this->db->query($query);
    }

您似乎没有正确归还它。试试这个。 (未测试)

$this->db->select('p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty');
$this->db->join('tbl_category AS c', 'p.id_Category = c.id_Category');
$this->db->join('tbl_brand AS b', 'p.id_brand = b.id_brand');

$query = $this->db->get_where('tbl_product AS p', array('p.id_product' => $prod_id));

return $query->result_array();

CodeIgniter 确实有一个查询绑定器可以实现类似的功能。我没有彻底查看您的 SQL 并且不知道它是否有效,但是这样的查询看起来像:

$this->db->select('p.id_product, p.product_name, p.price, p.discount, c.category, b.brand_name, p.description, p.spesification, p.picture, p.begin_date, p.end_date, p.qty')
      ->from('tbl_product as p, tbl_category as c, tbl_brand as b')
      ->where('p.id_Category = c.id_Category')
      ->where('p.id_brand = b.id_brand')
      ->where('id_product', $id_product);
      $query = $this->db->get();
      return $query;

我有点不清楚你想达到什么目的,我没有完全理解这个问题。您只是想让查询正常工作,还是正在寻求建议以尝试将所有信息集中在一行中?如果你想加入表格,你可以尝试这样的事情:

$this->db->select('*');
    $this->db->from('tbl_product');
    $this->db->join('tbl_category', 'tbl_category.id_category = tbl_products.id_category');
    $this->db->join('tbl_brand', 'tbl_brand.id_brand = tbl_products.id_brand', 'inner');
    $this->db->where('id_product', $id_product);
    $query = $this->db->get();
    return $query;