0
votes

I am stuck while implement inner join in codeigniter. it works fine without where condition. but when i use where condition it return empty result. Please Help me to find out the problem.

 $this->db->select('*');
 $this->db->from('user');
 $this->db->join('issue', 'user.id = issue.uid', 'inner');
 $this->db->where('issue.uid', $id);
 $query = $this->db->get()->result_array();
 return $query;

Here is the final query

 SELECT * FROM `tbl_user` INNER JOIN `tbl_issue` ON `tbl_user`.`id` = 
`tbl_issue`.`uid` WHERE `tbl_issue`.`uid` = '2'

Any solution appreciated!

2
have you checked, you have a matching record with uid in your db? - Uzair Khan
Get the Raw query using following function $this->db->last_query() and compare to see if this is the query you want or something else. - ascsoftw
yes @UzairKhan if i run query without where condition it return all records match in both table, but i need only one - user11636668
get a raw query as mentioned by @user1309690 to check if your query having the $id received or not - Uzair Khan
SELECT * FROM tbl_user INNER JOIN tbl_issue ON tbl_user.id = tbl_issue.uid WHERE tbl_issue.uid = '2' run this query in your phpmyadmin to check if you have any issue or you have a matching record. - Uzair Khan

2 Answers

0
votes

In My Model ::

public function innerjoindata($id)
{
    $this->db->select('*');
    $this->db->from('user');
    $this->db->join('issue', 'user.id = issue.uid', 'inner');
    $this->db->where('issue.uid', $id);
    $query = $this->db->get()->result_array();
    return $query;
}

RESULT ARRAY :::

Array ( [0] => Array ( [id] => 1 [username] => demouser [hobby] => reading [uid] => 1 [gym] => yes )

[1] => Array
    (
        [id] => 2
        [username] => demouser
        [hobby] => reading
        [uid] => 1
        [gym] => no
    )

)

Your Query is 100% Write I think Your Database Has No Relevant Data That's Why Create Problem Hope You it's help YOU

0
votes

As per the given code, you are providing the correct query. Please try to get directly from the second table with where condition.

SELECT * FROM tbl_issue WHERE tbl_issue.uid = '2'.