2

I am running a query with Active Record in a model of my CodeIgniter application, the query looks like this:

public function selectAllJobs()
{
    $this->db->select('*')
        ->from('job_listing')
        ->join('job_listing_has_employer_details', 'job_listing_has_employer_details.employer_details_id = job_listing.id', 'left');
        //->join('employer_details', 'employer_details.users_id = job_listing_has_employer_details.employer_details_id');
    
    $query = $this->db->get();
    return $query->result_array();
}

This returns an array that looks like this,

    [0]=>
  array(13) {
    ["id"]=>
    string(1) "1"
    ["job_titles_id"]=>
    string(1) "1"
    ["location"]=>
    string(12) "Huddersfield"
    ["location_postcode"]=>
    string(7) "HD3 4AG"
    ["basic_salary"]=>
    string(19) "£20,000 - £25,000"
    ["bonus"]=>
    string(12) "php, html, j"
    ["benefits"]=>
    string(11) "Compnay Car"
    ["key_skills"]=>
    string(1) "1"
    ["retrain_position"]=>
    string(3) "YES"
    ["summary"]=>
    string(73) "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
    ["description"]=>
    string(73) "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
    ["job_listing_id"]=>
    NULL
    ["employer_details_id"]=>
    NULL
  }
}

The job_listing_id and employer_details_id return as NULL. However if I run the SQL in phpmyadmin I get a full set of results.

The query I'm running in phpmyadmin is:

SELECT
    *
FROM
    (`job_listing`)
LEFT JOIN
    `job_listing_has_employer_details`
        ON `job_listing_has_employer_details`.`employer_details_id`
LIMIT
    0, 30

Is there a reason why I am getting differing results?

1
  • That's odd... Have you tried return $query->result(); instead of $query->result_array(), to see if the object version of the results presents the same problem? Commented Jun 6, 2010 at 19:46

1 Answer 1

3

Run the $this->db->last_query() and check the difference. It's a very useful debugging tool.

Of course the limit will have an effect, but I'm ignoring that.

It seems in your query in phpmyadmin, your 'ON' doesn't have a second part. I think your query in phpmyadmin should be:

SELECT *
FROM (
`job_listing`
)
LEFT JOIN `job_listing_has_employer_details` ON `job_listing_has_employer_details`.`employer_details_id` =  = `job_listing`.`id`

You should be using result_array, despite what the other person said.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.