0

My code is here SQL query:

$sql = "select * from products where status='1' AND country='5' AND product_price > "50" AND product_price <= "100" order by product_id desc"; $product = $this->db->query($sql)->result_array(); 

It returns all products by using customs SQL but still not showing order by desc.

5
  • Please post your table structure and data with expected output of query. Commented Dec 7, 2017 at 6:32
  • I have no problems expected output of query but problems is don't showing desc order. Commented Dec 7, 2017 at 6:34
  • try this, select * from products where status='1' AND country='5' AND product_price > '50' AND product_price <= '100' order by product_id desc Commented Dec 7, 2017 at 6:34
  • select * from products where status='1' AND country='5' AND product_price > '50' AND product_price <= '100' order by product_id desc still not showing. Commented Dec 7, 2017 at 6:37
  • Can you post the result of the query? Commented Dec 7, 2017 at 6:38

6 Answers 6

1

Try this:

$sql = "SELECT * FROM `products` WHERE `status`=1 AND `country`=5 AND `product_price` > 50 AND `product_price` <= 100 ORDER BY `product_id` DESC"; $product = $this->db->query($sql)->result_array(); 
Sign up to request clarification or add additional context in comments.

Comments

1

It's better to use built-in Codeigniter Query-builder:

$this->db->select('*'); $this->db->where([ 'status' => 1, 'country' => 5, 'product_price >' => 50, 'product_price <=' => 100, ]); $this->db->order_by('product_id', 'DESC'); $this->db->get('products'); $product = $this->db->result(); 

Comments

0

You can try this solution for your problem :

$query = $this->db->query('select * from products where status='1' AND country='5' AND product_price > "50" AND product_price <= "100" order by product_id desc'); $row_array = $query->result_array(); 

I Hop it will help you.

3 Comments

Ok, I will check.
Don't work! this query, in fact, this query same as my query.
Please post your table structure with datatype of each field
0

Try this

$this->db->from('products'); $this->db->where("status='1' AND country='5' AND product_price > '50' AND product_price <= '100'", null, false); $this->db->order_by("product_id", "desc"); $query = $this->db->get(); return $query->result_array(); 

1 Comment

I want to custom query.I don't use active records query.
0

Please try below code:

$this->db->select('products.*'); $this->db->from('products'); $sql="status='1' AND country='5' AND (product_price > '50' AND product_price <= '100')"; $this->db->where($sql, NULL, FALSE)->order_by("product_id", "DESC"); $products_query = $this->db->get(); $products_info_array = array(); $products_info_array = $products_query->result_array(); return $products_info_array; 

Comments

0

Can you "describe" type the table? If the field "product_id" is text (Example: VARCHAR), then it will never follow the order asc/desc..

Sorry My English.. Good work!

1 Comment

product_id field is "int(11), AUTO_INCREMENT and primary key"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.