专栏' id' where子句在codeigniter中是不明确的

时间:2015-08-28 13:09:02

标签: mysql codeigniter

我想在codeigniter中加入两个表。我想根据登录用户显示数据。我得到了user.it的会话ID,显示我在标题上提到的错误信息。当我评论" $this->db->where("id",$this->session->userdata['logged_in']['id']);"这一部分可以正常使用。

型号

function get_user(){

    $this->db->select("user.firstname,user.lastname,user.address, user.email, user.contact_no, project.location"); 
    $this->db->where("id",$this->session->userdata['logged_in']['id']); //check login user
    $this->db->from('user');
     $this->db->join('project', 'project.client_id = user.id');
    $query = $this->db->get();
    return $query->result();

控制器

public function index(){  

 $data['post'] = $this->profile_model->get_user(); // calling Post model method getPosts() 
        $this->load->view('user_include/header');
        $this->load->view('user_site/profile',$data);
    }

查看

<?php if($post) { ?> 
<?php foreach($post as $post){?> 

    <div><?php echo $post->firstname ; echo '&nbsp; &nbsp;'; echo $post->lastname;?></div> <br>
    <div><?php echo $post->address ; echo '&nbsp; &nbsp; &nbsp;  &nbsp;&nbsp; &nbsp; &nbsp;  &nbsp;'?></div><br>
    <div><?php echo $post->contact_no ; echo '&nbsp; &nbsp; &nbsp;  &nbsp;&nbsp; &nbsp; &nbsp;  &nbsp;'?></div> <br>
    <div><?php echo $post->email ; echo '&nbsp; &nbsp; &nbsp;  &nbsp;&nbsp; &nbsp; &nbsp;  &nbsp;'?></div> <br>
    <div><?php echo $post->location ; echo '&nbsp; &nbsp; &nbsp;  &nbsp;&nbsp; &nbsp; &nbsp;  &nbsp;'?></div> <br>

<?php } 
} else {

    ?> 
 </div>
<div clospan="4" align="center">No records found to display</div>

1 个答案:

答案 0 :(得分:2)

列对于数据库来说是不明确的。尝试更改where子句,如下所示:

    $this->db->where("user.id",$this->session->userdata['logged_in']['id']);

$this->db->where("project.id",$this->session->userdata['logged_in']['id']);

如果是这样的话。由于两个表都可能包含id列,因此您必须非常清楚您所引用的是哪一个。