【问题标题】:Column 'id' in where clause is ambiguous in codeigniterwhere 子句中的列 'id' 在 codeigniter 中不明确
【发布时间】:2015-08-28 13:09:01
【问题描述】:

我想在 codeigniter 中加入两个表。我想根据登录用户显示数据。我这样做是为了获取用户的会话 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>

【问题讨论】:

  • 试试$this-&gt;db-&gt;where("user.id",$this-&gt;session-&gt;userdata['logged_in']['id']);
  • 也许别名 where ID...where("user.id",$this-&gt;se 看来项目和用户可能都有表列ID
  • 感谢萨蒂。它工作正常

标签: mysql codeigniter


【解决方案1】:

数据库的列不明确。尝试像这样更改where 子句:

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

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

如果是这样的话。由于两个表都可能有一个id 列,所以你必须非常清楚你指的是哪一个。

【讨论】:

    猜你喜欢
    • 2014-05-03
    • 2021-05-30
    • 1970-01-01
    • 2012-01-20
    • 2017-07-22
    • 2019-04-05
    • 1970-01-01
    • 2011-06-11
    • 2019-10-09
    相关资源
    最近更新 更多