Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Codeigniter中连接两个表_Php_Sql_Codeigniter_Join - Fatal编程技术网

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
Try
echo$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