如何从CodeIgniter中具有相同列名的表中输出数据?

时间:2022-03-02 13:56:32

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):

但是如何输出结果呢?这有效,但只获得最后的描述(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)?

或者我应该重命名列(例如type_of_source_description)?

3 个解决方案

#1


5  

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.

这实际上与CodeIgniter关系不大,而且mysql_fetch_assoc如何提供查询结果。解决方案是您应该使用“AS”重命名查询中的列,例如

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

#2


2  

Just alias the columns in the SELECT statement. Do not modify your database. The standard practice is to use aliasing in your SQL statements.

只是别名SELECT语句中的列。不要修改您的数据库。标准做法是在SQL语句中使用别名。

From PostgreSQL's docs for "SELECT":

来自PostgreSQL的“SELECT”文档:

"Using the clause AS output_name, another name can be specified for an output column."

“使用AS output_name子句,可以为输出列指定另一个名称。”

PostgreSQL SELECT

#3


1  

I recommend not to use that way, instead you can rename output columns with "AS".

我建议不要使用那种方式,而是可以使用“AS”重命名输出列。

#1


5  

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.

这实际上与CodeIgniter关系不大,而且mysql_fetch_assoc如何提供查询结果。解决方案是您应该使用“AS”重命名查询中的列,例如

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

#2


2  

Just alias the columns in the SELECT statement. Do not modify your database. The standard practice is to use aliasing in your SQL statements.

只是别名SELECT语句中的列。不要修改您的数据库。标准做法是在SQL语句中使用别名。

From PostgreSQL's docs for "SELECT":

来自PostgreSQL的“SELECT”文档:

"Using the clause AS output_name, another name can be specified for an output column."

“使用AS output_name子句,可以为输出列指定另一个名称。”

PostgreSQL SELECT

#3


1  

I recommend not to use that way, instead you can rename output columns with "AS".

我建议不要使用那种方式,而是可以使用“AS”重命名输出列。