Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:54:56+00:00 2026-06-06T16:54:56+00:00

I have this following query for CodeIgniter: $q = $this->db->where(‘(message_from=’.$user_id.’ AND message_to=’.$this->auth_model->userdata[‘user_id’].’)’) ->or_where(‘(message_from=’.$this->auth_model->userdata[‘user_id’].’ AND

  • 0

I have this following query for CodeIgniter:

$q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")')
            ->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")')
            ->get('messages');

I want to write this query with completely active record.

I have tried something like this:

$from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']);
            $to_where   = array('message_from'=>$this->auth_model->userdata['user_id'],'message_to'=>$user_id);    
            $q = $this->db->where($from_where)
            ->or_where($to_where)
            ->get('messages');

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

The above code produces this query:

SELECT * FROM (`messages`) WHERE `message_from` = '2' AND `message_to` = '1' OR `message_from` = '1' OR `message_to` = '2'

But this is what I want to produce:

SELECT * FROM (`messages`) WHERE (message_from="2" AND message_to="1") OR (message_from="1" AND message_to="2")

There are similar questions here and here, but thosedid not provide a real solution for me.

How’s this possible, If not via core libraries, is there an extension which allows writing such queries?

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:54:57+00:00Added an answer on June 6, 2026 at 4:54 pm

    You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
    Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions

    public function _compile_select($select_override = FALSE)
    public function _reset_select()
    

    Now subquery writing in available And now here is your query with active record

    $this->db->where('message_from','2');
    $this->db->where('message_to','1');
    
    $subQuery1 = $this->db->_compile_select();
    $this->db->_reset_select();
    
    
    $this->db->where('message_from','1');
    $this->db->where('message_to','2');
    
    $subQuery2 = $this->db->_compile_select();
    $this->db->_reset_select();
    
    $this->db->select('*');
    $this->db->where("$subQuery1");
    $this->db->or_where("$subQuery2");
    $this->db->get('messages');
    

    Look at this answer of mine. This shows how to use sub queries. This will help
    Using Mysql WHERE IN clause in codeigniter

    EDITES

    Yes i have done it
    Rewrite the query this way exactly you want

    $this->db->where('message_from','2');
    $this->db->where('message_to','1');
    
    $subQuery1 = $this->db->_compile_select(TRUE);
    $this->db->_reset_select();
    
    
    $this->db->where('message_from','1');
    $this->db->where('message_to','2');
    
    $subQuery2 = $this->db->_compile_select(TRUE);
    $this->db->_reset_select();
    
    $this->db->select('*');
    $this->db->where("($subQuery1)");
    $this->db->or_where("($subQuery2)");
    $this->db->get('messages');
    

    Compile select is with true parameter. Will not produce select clause. This will produce

    SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query in codeigniter The idea is to only get distinct
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have the following query, now the strange thing is if I run this
I have managed to bring the following output with this query SELECT DISTINCT IF(purchaseproduct_customerid=0,contactperson,vendorname)
I have the following query SELECT * FROM (`user_profiles`) WHERE `user_id` = $user_id LIMIT
I have the following query: $select = $this->getDao()->select() ->from( array(new Zend_Db_Expr('FROM_UNIXTIME(expiration)')) ); The getDao
I have following query: THis is my structure in some .h file typedef struct
I have this following query: SELECT a.rank AS rank, concat( m.prenom, ' ', m.nom
Good day, I have this weird problem: This following statement works Query q =
I wrote the following query to calculate the sum. How can also have this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.