Welcome Guest, Not a member yet? Register   Sign In
Problems with where_not_in
#1

[eluser]webnology[/eluser]
Hi all,

I have a table with bands, and a table with genres. I also have a table that links both. So aband can have multiple genres. Now I want to show all genres that have not been assign to aband yet.

So I'm trying this: Get all genres in the bands2genre table, and the use that array as an exclusion for the genre table.

Code:
//Get all genres, except those assigned to the band
    function getAllGenres($bandid) {
    
        $this->db->select('genre_id');
        $this->db->where('band_id', $bandid);
        $this->db->from('bands2genre');
        
        $assigned_genres = $this->db->get();
        
        $this->db->select('*');
        $this->db->from('genres');
        $this->db->where_not_in('genre_id', $assigned_genres);
        
        //Get the records according to the above SQL statements
        $Q = $this->db->get();

        return $Q;
    
    }

However, Im having errors: like this:

Code:
A PHP Error was encountered

Severity: Notice

Message: Object of class CI_DB_mysql_result to string conversion

Filename: database/DB_active_rec.php

Line Number: 586
A Database Error Occurred

Error Number: 1054

Unknown column 'Object' in 'where clause'

SELECT * FROM (`genres`) WHERE `genre_id` NOT IN (Object)

So, is my $assigned_genres not an array?

Any help appreciated!

Michel
#2

[eluser]xwero[/eluser]
get returns the query object not the actual result. The where_not_in expects an array as second parameter so the code changes are
Code:
$assigned_genres = $this->db->get();
// becomes
$query = $this->db->get();
$rows = $query->result();
$ids = array();
foreach($rows as $row)
{
   $ids[] = $row->genre_id
}
/* and */
$this->db->where_not_in('genre_id', $assigned_genres);
// becomes
$this->db->where_not_in('genre_id', $ids);
#3

[eluser]webnology[/eluser]
Great, thx man.




Theme © iAndrew 2016 - Forum software by © MyBB