2016-12-27 34 views
1

我對Codeigniter頗爲陌生。我有一個問題,看起來我的查詢不起作用。是因爲我的格式嗎?CodeIgniter用於從不同表中獲取數據的SQL正確格式

型號

$this->db->select('*'); 
    $this->db->from(' 
    store_products, 
    products, 
    category, 
    subcategory, 
    store, 
    store_products_category'); 
    $this->db->where('store_products_category.store_id', $marketid); 
    $this->db->where('store_products_category.subcategory_id', 'subcategory.subcategory_id'); 
    $this->db->where('subcategory.category_id', 'category.category_id'); 
    $this->db->where('store_products_category.storeprod_id', 'store_products.storeprod_id'); 
    $this->db->where('store_products.prod_id', 'products.prod_id'); 
    $this->db->where('store_products_category.store_id', 'store.store_id'); 
    $query = $this->db->get(); 
    $result = $query->row(); 

    return $result; 

控制器

if($marketinfo = $this->MarketModel->getInfobyID($marketid)){ 
    $data['marketinfolist'] = $marketinfo; 
    $this->load->view('layouts/header', $data); 
    $this->load->view('clickbasket',$data); 
    $this->load->view('navigation/mainfooter'); 
    $this->load->view('layouts/footer'); 
} 

看來,它不能從模型返回任何東西。我已經嘗試直接在phpmyadmin上執行查詢,並且它完美地工作。

回答

2

您需要聯接表來獲得正確的結果......這樣的..

模型:

$query = $this->db->select('*') 
       ->from('store_products') 
       ->join('products', 'store_products.prod_id = products.prod_id') 
       ->join('store_products_category', 'store_products_category.storeprod_id = store_products.storeprod_id') 
       ->join('subcategory', 'store_products_category.subcategory_id = subcategory.subcategory_id'); 
       ->join('category', 'subcategory.category_id = category.category_id') 
       ->join('store', 'store_products_category.store_id = store.store_id') 
       ->where('store_products_category.store_id', $marketid) 
       ->get(); 
$result = $query->row(); 
return $result; 

而在你的控制器..

function getMarketInfo($marketid) 
    { 
    if(!empty($marketid)){ 
    $marketinfo = $this->MarketModel->getInfobyID($marketid); 
    $data['marketinfolist'] = $marketinfo; 
    $this->load->view('layouts/header', $data); 
    $this->load->view('clickbasket',$data); 
    $this->load->view('navigation/mainfooter'); 
    $this->load->view('layouts/footer'); 
    } 
    } 
+0

謝謝它現在工作正常:)只是不得不調整$ query-> row()部分 –

1

如果我正確地理解你的問題,那麼它會發生,因爲不正確的連接已被應用,並且代碼點火器不能夠生成正確的查詢。試試這個:

$query = $this->db->select('*') 
       ->from('store_products') 
       ->join('products', 'store_products.prod_id = products.prod_id') 
       ->join('store_products_category', 'store_products_category.storeprod_id = store_products.storeprod_id') 
       ->join('subcategory', 'store_products_category.subcategory_id = subcategory.subcategory_id'); 
       ->join('category', 'subcategory.category_id = category.category_id') 
       ->join('store', 'store_products_category.store_id = store.store_id') 
       ->where('store_products_category.store_id', $marketid) 
       ->get(); 
$result = $query->row(); 

此外,您還可以通過下面的語句瀏覽器打印查詢,然後可以複製並在mysql中執行它來測試它:

echo $this->db->last_query(); 

它打印由當前執行的最新查詢模型。

相關問題