Php 如何在Codeigniter中连接两个表
我尝试连接两个表并输出结果: 在模型中:Php 如何在Codeigniter中连接两个表,php,sql,codeigniter,join,Php,Sql,Codeigniter,Join,我尝试连接两个表并输出结果: 在模型中: $this->db->select('*'); $this->db->from('news'); $this->db->join('authors', 'authors.id = news.author_id'); 在视图文件中: foreach($item as $row): echo $row->id; endforeach; 当我运行上面的代码时,它将从authors表中输出id列。 我的问题是
$this->db->select('*');
$this->db->from('news');
$this->db->join('authors', 'authors.id = news.author_id');
在视图文件中:
foreach($item as $row):
echo $row->id;
endforeach;
当我运行上面的代码时,它将从authors
表中输出id
列。
我的问题是如何从news
表中回显id
列
news
和author
表都有一列名为id
Tryecho$row->author\u id
似乎您想要在新闻表中显示的字段是author\u id
,而不是id
编辑否则,您可以在select语句中使用as
sql关键字为字段指定不同的名称
$this->db->select('*, news.id as my_news_id');
然后执行,
echo$row->my_news\u id
我想从新闻表中回显id