2015-03-18 124 views
0

我需要你們的幫助,我的表像這樣table笨查詢生成器加入3表

我要查看產品詳細點擊與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); 
    } 

回答

2

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; 
+0

它的工作.. thx男子..它真的很有幫助 – 2015-03-18 21:30:42

0

看起來你沒有正確地返回它。嘗試這個。 (未測試)

$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();