CodeIgniter Forums
Selecting from database based on another query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Selecting from database based on another query (/showthread.php?tid=75372)



Selecting from database based on another query - jldesign - 01-30-2020

What I am trying to do is search my privileges table where db_id column holds which database a user has rights to.  From that database I want to pull all the user_id that have a db_id... ie: 2.

I then want to search my user table for those user_id and list all of their details.

The problem I can not solve is getting the array of user_ids in where where statement.
Please see the included code.  Any advice would be greatly appreciated.
Code:
function return_users(){
            
                $this->db_core->where('db_id', $this->session->userdata("db_id"));
                $this->db_core->select('user_id');
                $query = $this->db_core->get('privileges');
    
                
                foreach($query->result() as $row) {
                    $priv[] = $row->user_id;
                    };

                $this->db_core->where('user_id', $priv);
                $list_users = $this->db_core->get('users');
                $list_users->result_array();
                
                return $list_users->result();
        }



RE: Selecting from database based on another query - nicojmb - 01-31-2020

(01-30-2020, 05:49 PM)jldesign Wrote: What I am trying to do is search my privileges table where db_id column holds which database a user has rights to.  From that database I want to pull all the user_id that have a db_id... ie: 2.

I then want to search my user table for those user_id and list all of their details.

The problem I can not solve is getting the array of user_ids in where where statement.
Please see the included code.  Any advice would be greatly appreciated.
Code:
function return_users(){

$this->db_core->where('db_id', $this->session->userdata("db_id"));
$this->db_core->select('user_id');
$query = $this->db_core->get('privileges');


foreach($query->result() as $row) {
$priv[] = $row->user_id;
};

$this->db_core->where('user_id', $priv);
$list_users = $this->db_core->get('users');
$list_users->result_array();

return $list_users->result();
}

Hi, to search in array filed, you need to change to "where_in":

PHP Code:
$this->db_core->where('user_id'$priv); 



RE: Selecting from database based on another query - InsiteFX - 01-31-2020

Try this one.

PHP Code:
$this->db_core->where_in('user_id'$priv);