小编典典

CodeIgniter中的联接查询

sql

这个问题已经在这里有了答案

在同一网页上显示两个表的数据 (1个答案)

7年前关闭。

我在CodeIgniter中使用联接查询,但无法使其正常工作。它仅显示一个表数据,而不显示其他表数据。我是CodeIgniter的新手,无法解决此问题。请别人帮我。提前谢谢。

看法

<?php foreach ($query as $row): ?>

    <?php echo $row->answerA;?><br>
    <?php echo $row->answerB;?><br>
    <?php echo $row->answerC;?><br>
    <?php echo $row->comment;?><br>
    <?php echo $row->name; ?>

<?php endforeach; ?>

控制器

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

}

模型

function result_getall() {

    $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'right outer'); 
    $query = $this->db->get();
    return $query->result();

}

编辑

的结果

print_r($data['query']);
die();

是一个如下数组:

Array ( [0] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 83 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => ledvinas@yahoo.ie ) [1] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 84 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => ledvinas@yahoo.ie ) [2] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 85 [name] => Edvinas [second_name] => Liutvaitis [phone] => [email] => ledvinas@yahoo.ie ) [3] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 86 [name] => EdvinasQ [second_name] => LiutvaitisQ [phone] => 12345678 [email] => ledvinas@yahoo.ie ) [4] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 87 [name] => Edvinas [second_name] => Liutvaitis [phone] => 123456 [email] => ledvinas@yahoo.ie ) )

表结构

证书

cid(PRIMARY),姓名,second_name,电话,电子邮件

tblanswers

answerid(PRIMARY),userid,questionid,answerA,answerB,answerC。


阅读 229

收藏
2021-03-23

共1个答案

小编典典

试试这个:

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid');

或者

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'inner');

并打印结果以查看是否要

2021-03-23