php - MYSQL Query to Codeigniter Query -
i have query below works fine when use in phpmyadmin, bit unsure how within ci framework when have m.id
etc in place.
query:
select distinct m.name, m.id, c.id, c.name `default_ps_products` p inner join `default_ps_products_manufacturers` m on p.manufacturer_id = m.id inner join `default_ps_product_x_cats` x on p.id = x.product_id inner join `default_ps_products_categories` c on x.category_id = c.id
there many ways.
example 1:
$result = $this->db ->select('m.name, m.id, c.id, c.name') ->distinct() ->join('default_ps_products_manufacturers m', 'p.manufacturer_id=m.id') ->join('default_ps_product_x_cats x', 'p.id=x.product_id') ->join('default_ps_products_categories c', 'x.category_id=c.id') ->get('default_ps_products p') ->result(); echo $this->db->last_query();
sometimes active record can't produce query want. can write yourself.
example 2:
$query = "select distinct m.name, m.id, c.id, c.name `default_ps_products` p inner join `default_ps_products_manufacturers` m on p.manufacturer_id = m.id inner join `default_ps_product_x_cats` x on p.id = x.product_id inner join `default_ps_products_categories` c on x.category_id = c.id"; $result = $this->db ->query($query) ->result(); echo $this->db->last_query();
in second example, db::query() can take array second parameter replace question marks (?) within $query respective value. example needed add values query.
example 3:
$query = "select distinct m.name, m.id, c.id, c.name `default_ps_products` p inner join `default_ps_products_manufacturers` m on p.manufacturer_id = m.id inner join `default_ps_product_x_cats` x on p.id = x.product_id inner join `default_ps_products_categories` c on x.category_id = c.id c.id=?"; $result = $this->db ->query($query, array(1)) ->result(); echo $this->db->last_query();
Comments
Post a Comment