我对Codeigniter很新。我有一个问题,似乎我的查询不起作用。是因为我的格式吗?
模型
$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上进行查询,它运行正常。
答案 0 :(得分: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');
}
}
答案 1 :(得分: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();
它打印当前模型执行的最新查询。