php CodeIgniter 连接两个表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15202543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter join two tables
提问by Edvinas Liutvaitis
I am trying to join two tables together using CodeIgniter. I used CodeIgniter user guide for help. I am having some issues where only one table's data is displayed and I don't know why. Can someone help me out?
我正在尝试使用 CodeIgniter 将两个表连接在一起。我使用 CodeIgniter 用户指南寻求帮助。我遇到了一些问题,只显示一个表的数据,我不知道为什么。有人可以帮我吗?
Here is my code:
这是我的代码:
Controller
控制器
function getall(){
$this->load->model('result_model');
$data['query'] =
$this->result_model->result_getall();
$this->load->view('result_view', $data);
}
Model
模型
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();
}
View
看法
<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>
回答by Nish
function result_getall(){
$this->db->select('tblanswers.*,credentials.*');
$this->db->from('tblanswers');
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'left');
$query = $this->db->get();
return $query->result();
}
回答by Sachin Prasad
Try this in controller and see whats the result.Also,show us if you are getting any error and make sure there is data in your table :).
在控制器中试试这个,看看结果是什么。另外,如果你有任何错误,请告诉我们,并确保你的表中有数据:)。
Controller
控制器
function getall(){
$this->load->model('result_model');
$data['query'] =$this->result_model->result_getall();
print_r($data['query']);
die();
$this->load->view('result_view', $data);
}

