8

I am using join query in CodeIgniter and can't get it to work. It only displays one table data, but not the other. I am new to CodeIgniter and can't figure this out. Pleas someone help me. Tnanks in advance.

view

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

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

}

model

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

}

EDIT

The result of

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

is an array as below:

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

table structure

credentials

cid(PRIMARY), name, second_name, phone, email

tblanswers

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

4

4 回答 4

17

试试这个:

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

或者

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

并打印结果,看看是否是你想要的

于 2013-03-05T07:53:56.363 回答
5

您要查询的主表是什么?好吧你可以试试这个,建议在使用joins时,必须为每个表指定一个别名:

 function result_getall(){

    // if you want to query your primary data from the table 'tblanswers',
    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('tblanswers a');
    $this->db->join('credentials b', 'b.cid = a.answerid', 'left'); 
    $query = $this->db->get();
    return $query->result();

    // if you want to query your primary data from the table 'credentials',

    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('credentials a');
    $this->db->join('tblanswers b', 'b.answerid = a.cid', 'left'); 
    $query = $this->db->get();
    return $query->result();

}
于 2013-03-05T09:47:01.087 回答
2

删除 select() 标记,因为您需要所有术语。

你可以修改代码

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'outer'); 
$query = $this->db->get('tblanswers');
return $query->result();
于 2013-03-05T08:11:17.430 回答
0

如果你的两个表都有相关的,那么它应该可以工作。你为什么使用右外连接?只需使用内部联接或任何其他联接即可。请在此处阅读有关不同类型的 JOIN http://www.w3schools.com/sql/sql_join.asp 和此处http://dev.mysql.com/doc/refman/5.0/en/join.html

希望这会有所帮助。

谢谢

于 2013-03-05T07:56:00.280 回答