Search code examples
phpmysqlcodeigniterinner-joinmariadb

Joins Query not working in Codeigniter


public function getQuestions($params = "",$page= "all", $count=false){
$this->db->query('SELECT  questions.questions_id, questions.question_description,
        questions.question_explanation, questions.created_date,
        questions.updated_date, questions.is_active,
        diffLevels.difficulty_levels_title
    FROM  '.TBL_QUESTION.' as questions
    INNER JOIN  '.TBL_DIFFICULTY_LEVELS.' as diffLevels
           ON questions.fk_difficulty_levels_id = diffLevels.preference
    WHERE  questions.is_active=1');
            $Q = $this->db->get();
            if ($Q->num_rows() > 0) { 
                foreach ($Q->result() as $row) { 
                    $data[] = $row; 
                } 
            } 
            $Q->free_result(); 
            return $data;

        }

This is my Query..and I did few many tweaks but it wont work..waiting for possible solutions? Thanks


Solution

  • Try this Query:

    $this->db->select('
            questions.questions_id,
            questions.question_description,
            questions.question_explanation,
            questions.created_date,
            questions.updated_date,
            questions.is_active,
            diffLevels.difficulty_levels_title 
            ');
            $this->db->from("questions");
            $this->db->join("diffLevels",'questions.fk_difficulty_levels_id = diffLevels.preference' , 'inner');
            $this->db->where("questions.is_active",1);
            $query=$this->db->get();
            $data=$query->result_array(); 
            //echo $this->db->last_query();
            //echo "<hr/>";
            //echo "<pre>";
            //print_r($query);
            //exit;
    

    I have directly used table name as "questions" and "diffLevels",Please change accordingly.