Php CodeIgniter SQL用于从不同表获取数据的正确格式

Php CodeIgniter SQL用于从不同表获取数据的正确格式,php,mysql,codeigniter,Php,Mysql,Codeigniter,我对Codeigniter是个新手。我有一个问题,我的查询似乎不起作用。是因为我的格式吗 型号 $this->db->select('*'); $this->db->from(' store_products, products, category, subcategory, store, store_products_category'); $this->db->where('store_product

我对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上执行查询,它工作得非常好。

如果我正确理解了您的问题,那么它是由于应用了不正确的联接而发生的,并且代码点火器无法生成正确的查询。试试这个:

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

它打印当前模型执行的最新查询。

您需要加入表以获得正确的结果…如下所示

您的型号:

$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');
     }
   }
谢谢,现在一切正常:)只需调整$query->row()部分