php - where 子句中的列 'id' 对于 3 个表不明确 Codeigniter

标签 php mysql codeigniter

我目前正在做一个系统。在系统中,有一个名为manage resident的模块;如果单例,已婚和有 child (例如名字,中间名和姓氏),管理居民将获得该居民的信息。

我已经完成添加或创建新常驻部分,我现在正在编辑常驻部分。当我点击编辑常驻时,遇到了错误

Column 'id' in where clause is ambiguous

我有 3 个表:居民表、已婚表和 child 表。 Resident ID是married表的外键,Married ID是children表的外键。

我的每个表的数据示例(列主键,外键,名字,中间名,姓氏

Resident -> id, Testing, Testing, Testing
Married  -> Married_id, resident_id, Testing, Testing, Testing
Children -> id, married_id, Testing, Testing, Testing

问题:我怎样才能得到选中的居民的 child ?然后在我的 View 中单独显示在我的字段中。

Controller

 $id = $this->uri->segment(4);
 $get_children = $this->Crud_model->get_children($id);

型号

public function get_children($id){
        $this->db->select('*');    
        $this->db->from('resident');
        $this->db->where('id',$id);
        $this->db->join('married', 'resident.id = married.resident_id');
        $this->db->join('children', 'resident.id = children.married_id');
        $query = $this->db->get();
        return $query->row();
    }

查看

 <?php $explode = explode(",",$children->first_name); ?>

<div class="form-group">
  <label>First Name</label>
  <input type="text" class="form-control" name="child_fname" id="child_fname"><?= $explode?>
</div>

最佳答案

每当您使用 where 条件连接表时,您应该在每一列中写入其引用表名。

所以改变

$this->db->where('id',$id);

$this->db->where('resident.id',$id);

关于php - where 子句中的列 'id' 对于 3 个表不明确 Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47072825/

相关文章:

php - 遍历缺少索引的 Json 文件

php - 如何继续检查文件直到它存在,然后提供指向它的链接

mysql - 并行上传数据到mysql数据库

sql - mySQL 中的空值

php - 如何通过 SimpleXMLElement 中的 [@attributes]?

php - 可以通过 PHP PDO 快速 Access Access 吗?

mysql - SQL-根据另一列的内容查找最大值

php - 如何在 Ubuntu 14.04 Lamp Stack 服务器上的 Codeigniter 中设置 .htaccess

php - 使用 Codeigniter 中的最后插入 ID 生成唯一的代码?

php - PHP中用单个函数修改多个变量