2013-03-05 77 views
2

我在codeigniter中使用連接函數並且無法讓它工作它只顯示一個表數據但不是其他新的codeigniter無法找出這個請求有人幫我提前tnx。在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(); 

    } 

EDIT

這是

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] => [email protected]) [1] => stdClass Object ([answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 84 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => [email protected]) [2] => stdClass Object ([answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 85 [name] => Edvinas [second_name] => Liutvaitis [phone] => [email] => [email protected]) [3] => stdClass Object ([answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 86 [name] => EdvinasQ [second_name] => LiutvaitisQ [phone] => 12345678 [email] => [email protected]) [4] => stdClass Object ([answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 87 [name] => Edvinas [second_name] => Liutvaitis [phone] => 123456 [email] => [email protected])) 

表結構

憑證

CID(PRIMARY),名稱,秒ond_name,電話,電子郵件

tblanswers

answerid(主),用戶ID,questionid,answerA,answerB,answerC。

+0

訪問頁面roytuts.com/codeigniter-join-example/ – user3470953 2016-05-28 03:41:42

回答

7

嘗試這樣的:

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

或者

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

並打印結果,看看是否是你想要

+0

他們都返回一個空數組任何其他建議? TNX爲您提供幫助。 – 2013-03-05 07:59:47

0

什麼如果您兩臺表都具有相關的,那麼它應該管用。你爲什麼使用正確的外連接? 只需使用Inner Join或任何其他聯接,它就可以工作。請閱讀有關不同類型的加入這裏http://www.w3schools.com/sql/sql_join.asp和這裏http://dev.mysql.com/doc/refman/5.0/en/join.html

希望這會有所幫助。

謝謝

+0

內部連接返回一個空的數組,我離開了,我只是在玩連接選項,看看哪一個會爲我工作。我嘗試了所有的選項,我的東西我是那些是int用戶指南,並沒有一個返回兩個表在同一時間其以太一個或另一個。 – 2013-03-05 08:02:25

+0

你可以添加數據庫表的屏幕截圖,所以我可以看到數據?如果所有連接都沒有返回數據,那麼顯然問題將出現在數據排列或查詢中。查詢似乎很好,但仍不能說任何事情。請分享這兩張表的屏幕截圖,以便我可以看到兩張表的數據。 – 2013-03-05 08:49:29

+0

我編輯的問題,並把表結構。 – 2013-03-05 08:58:01

1

選擇刪除()標籤,爲u需要的所有條件。

U可以修改像

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'outer'); 
$query = $this->db->get('tblanswers'); 
return $query->result(); 
+0

我試過yuor代碼,我得到的是一個錯誤,這裏是錯誤 發生數據庫錯誤 錯誤號碼:1064 您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在'LEFT JOIN'憑證附近使用正確的語法'ON'tblanswers'.'answerid' ='credentials'.''' at line 2 SELECT * LEFT JOIN'文件名:C:\ wamp \ www \ Surva \ system \ database \ DB_driver.php 行號:330 – 2013-03-05 08:14:39

+0

已將您從'刪除' ' 標籤 ?它也需要被刪除 – 2013-03-05 09:36:42

0

代碼什麼是你想要的主表查詢?那麼你可以試試這個,建議使用聯接時,你必須爲每個表指定別名:

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

}