• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selecting from database based on another query

#1
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();
        }
Reply

#2
(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); 
Reply

#3
Try this one.

PHP Code:
$this->db_core->where_in('user_id'$priv); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.