Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
function get_result_professions($key_word)  
{  
    $sql = "SELECT users.name FROM users 
            inner join users_has_professions  on users_has_professions.users_id  = users.id 
            inner join professions on users_has_professions.professions_id  = professions.id  
            where professions.key_word = ?  ";


    return   $this->db->get()->query($sql, $key_word);
} 

When I execute this code I receive the following error:

A Database Error Occurred

Error Number: 1096

No tables used

SELECT *

Filename: /var/www/expertt/models/search_model.php

Line Number: 31

How can I solve this problem? Thanks in advance.

share|improve this question
add comment

2 Answers

$this->db->get() must contain an table name. in your case you want to remove it sindse you have an custom query so your function wil look like this:

function get_result_professions($key_word)  
{  
    $sql = "SELECT users.name FROM users 
            inner join users_has_professions  on users_has_professions.users_id  = users.id 
            inner join professions on users_has_professions.professions_id  = professions.id  
            where professions.key_word = '$key_word'  ";


    return   $this->db->query($sql);
} 
share|improve this answer
add comment

The $this->db->get() method in CodeIgniter's Active record requires a table name parameter (see the Active Record Documentation for more info) when used to query a table, unless you have previously build up the query using one of the other provided methods.

Usually when building up joins like you are doing you would use the select/ join methods provided by Active Record, like so

$this->db->select('users.name')->from('users')
    ->join('users_has_professions', 'users_has_professions.users_id  = users.id')
    ->join('professions', 'users_has_professions.professions_id  = professions.id')
    ->where('professions.key_word', $key_word); 

(untested as I don't have your database to run it against)

You can then use the $this->db->get() method to retrieve the results like so

$results = $this->db->get();
foreach($query->result() as $row) {
    //code here
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.