Search code examples
sqldatabasecodeigniter

How to output data from tables with same column names in CodeIgniter?


This is my query:

$query = $this->db->query('
    SELECT archives.id, archives.signature, type_of_source.description, media_type.description, origin.description 
    FROM archives, type_of_source, media_type, origin                                                             
    WHERE archives.type_of_source_id = type_of_source.id                                                          
    AND type_of_source.media_type_id = media_type.id                                                              
    AND archives.origin_id = origin.id                                                                            
    ORDER BY archives.id ASC
');

But how to output the result? This works, but only gets the last description (origin.description):

foreach ($query->result_array() as $row)
{
    echo $row['description'];
}

This doesn't work:

foreach ($query->result_array() as $row)
{
    echo $row['type_of_source.description'];
}

Or should I rename the columns (e.g. type_of_source_description)?


Solution

  • This actually has very little to do with CodeIgniter and a lot with how mysql_fetch_assoc provides the query results.

    The solution is that you should rename the columns inside the query using "AS", e.g.

    select type_of_source.description as type_of_source_description, origin.descripotion as origin_descripotion from ....