【发布时间】:2013-03-04 13:21:08
【问题描述】:
我正在尝试使用 CodeIgniter 将两个表连接在一起。我使用 CodeIgniter 用户指南寻求帮助。我遇到一些问题,只显示一个表的数据,我不知道为什么。有人可以帮帮我吗?
这是我的代码:
控制器
function getall(){
$this->load->model('result_model');
$data['query'] =
$this->result_model->result_getall();
$this->load->view('result_view', $data);
}
型号
function result_getall(){
$this->db->select('*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left');
$query = $this->db->get();
return $query->result();
}
查看
<div>
<?php foreach ($query as $row): ?>
//tblanswers
<?php echo $row->answerA;?><br>
<?php echo $row->answerB;?><br>
<?php echo $row->answerC;?><br>
<?php echo $row->comment;?><br>
//credentials
<?php echo $row->name; ?>
<?php endforeach; ?>
</div>
【问题讨论】:
-
使用 echo $this->db->last_query() 查看生成的查询。
-
这是生成的查询 SELECT * FROM (
tblanswers) LEFT JOINcredentialsONtblanswers.answerid=credentials.cid -
使用 var_dump($query->result())..它是否显示第二个表数据?这个输出是什么?
-
你能在复制表上解释更多吗?
标签: php codeigniter join