views:

40

answers:

1

I am trying to figure out why my JOIN is not working on a database query, I have two tables with a user_ID column, but when I return this it returns everything, not just the selected ones per user. What am I messing up any thoughts? Thank you!

function user_apps()
{
    $this->db->select('*');
    $this->db->from('apps');
    $this->db->join('members', 'members.user_ID = apps.user_ID');

    $query = $this->db->get();

    return $query;

}

Here is an image of the DB tables, and goal is to basically get the urls from the apps table of each user, http://cl.ly/516bd1e8aae62bd11773

+1  A: 

You haven't specified a WHERE clause in your query. At the moment it is returning everything because you only have two users and they both have entries in the 'apps' table. If you add:

$this->db->where('members.first_name', $first_name);

And pass a first name as a parameter to the function then it will only return results for that user.

function user_apps($first_name)
{
$this->db->select('*');
$this->db->from('apps');
$this->db->join('members', 'members.user_ID = apps.user_ID');
$this->db->where('members.first_name', $first_name);

$query = $this->db->get();

return $query;

}
musoNic80
Thanks! Seems to be working now :)
thatryan
No problem. Of course, filtering results only by first name probably isn't very sensible in a real-world situation. I'd suggest making users register a unique username and then filter using that.
musoNic80
Yeah I did, I set their session data to have their user id also and pass that in. ;)
thatryan