PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 28, 2022

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

 February 28, 2022     codeigniter, database, sql     No comments   

Issue

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 ....


Answered By - pilsetnieks
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing