1

The following is my mysql query:

select * from db_posts LEFT JOIN db_followers  ON db_posts_user_id = db_followers_following AND db_followers_user_id = 276

How can I convert it to codeigniter using query builder?

$this->db->where('db_posts_user_id', 'db_followers_following');
$this->db->where('db_followers_user_id', 276);
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id');
$query2 = $this->db->get('db_posts')->result_array();

I get the required result when I use mysql query. But I get a blank array when I use codeigniter queries. What is wrong in my CI Query?

5
  • 1
    oops i forgot that. but still i get only 10 rows as result while i have total 400 rows in db_posts. is there any mistakes in my query? Commented Nov 20, 2018 at 11:25
  • 2
    $this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id', 'Left'); Commented Nov 20, 2018 at 11:26
  • 1
    also i guess that is not mandatory as i get same result when i omit that statement also! Commented Nov 20, 2018 at 11:27
  • 2
    there is something wrong with your $this->db->where('db_posts_user_id', 'db_followers_following'); $this->db->where('db_followers_user_id', 276); Please verify it Commented Nov 20, 2018 at 11:28
  • 3
    First, Try your query without any conditions. And then use 1 condition and then second. It will tell you where is the actual problem. Commented Nov 20, 2018 at 11:33

2 Answers 2

3

why are you using those where clause? if you are using it as condition for your 'JOIN' process, try it like this:

$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id and db_followers_user_id = 276', 'left');
$query2 = $this->db->get('db_posts')->result_array();

Points to Note

1)as previous comments said, you should 'Left Join' if you want to get all posts from 'db_posts'.

2) You can add Multiple conditions in ON Clause using and but within ''. all your conditions should be specified before second comma(,) which is before mentioning 'LEFT'.

Hope this will help. Lemme know if this help

Sign up to request clarification or add additional context in comments.

3 Comments

Lemme try it :)
That worked man. Yous saved my hours. i never thought we could use and conditions in 'join' query builder. thanks man :) Accepting this answer
Yeah we can use 'AND' conditions. Happy to know it worked for you :)
3

Try adding the "Left" option to tell it what kind of join to do:

$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id', 'Left');

Also I'm not sure you need

$this->db->where('db_posts_user_id', 'db_followers_following');
  • this is already covered by your JOIN statement.

Source: http://www.bsourcecode.com/codeigniter/codeigniter-join-query/#left-join and https://www.codeigniter.com/userguide3/database/query_builder.html

1 Comment

oo lemme try that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.