CodeIgniter Forums

Full Version: Join is not behaving as expected.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]CloakandPigeon[/eluser]
Hello, kind of new here and just wanted to post up an issue I am running into currently. I am attempting a join between an articles and users table so that I can pull the username column instead of printing out an ID number (it isn't very helpful to just have a number when trying to determine who the article belongs to).

I have the following code in my model:

Code:
$this->db->select('articles.*', 'users.*');
  $this->db->from('articles');
  $this->db->order_by('articleID', 'asc');
  $this->db->join('users', 'users.id = articles.userID');
  $query=$this->db->get()->result();
  return $query;

All fields are available to me when I pass it to the view from the articles table, but none of them are available from the users table. I have attempted with the 'left' parameter added as well. Am I using the join command properly?

I am wanting to get a table with columns for everything in articles and added to that I want to be able to pull the username info from the users table.

El Forum

[eluser]Aken[/eluser]
Your select() method is wrong. Items are not passed as parameters; they should either be a string or an array in the first parameter. Check the docs. Smile

El Forum

[eluser]CloakandPigeon[/eluser]
[quote author="Aken" date="1361577294"]Your select() method is wrong. Items are not passed as parameters; they should either be a string or an array in the first parameter. Check the docs. Smile[/quote]

Yeh, I think I messed that up after attempting a few different solutions I found while researching (none of which worked). I originally had it set to select all (select(*)).

From your experience does my join statement look like it would work for what I am wanting?

Thanks again for your help.

El Forum

[eluser]CloakandPigeon[/eluser]
Nevermind, I am an idiot and Aken was correct, my select statement was messing up my query.

Thanks Aken!