Codeigniter 3:where子句给出歧义的列错误

时间:2019-01-19 15:22:45

标签: php mysql codeigniter-3

我正在使用Codeigniter 3.1.8和Bootstrap 4中的基本博客应用

有几个表,其中authorspostscategories用于存储帖子类别。

我正在显示带有每个帖子所属类别名称的帖子。

我还想显示按作者过滤的帖子。换句话说,作者撰写的所有帖子。为此,我在Posts_model模型中创建了一个方法:

public function get_posts_by_author($author_id) {
    $this->db->select('posts.*,categories.name as post_category');
    $this->db->order_by('posts.id', 'DESC');
    $this->db->join('categories', 'posts.cat_id = categories.id', 'inner');
    $query = $this->db->get_where('posts', array('author_id' => $author_id));
    return $query->result();
}

Posts控制器中,我有方法:

public function byauthor($author_id){
    $data = $this->Static_model->get_static_data();
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories(); 
    $data['posts'] = $this->Posts_model->get_posts_by_author($author_id);
    echo  $author_id;
}

/posts/byauthor/1上,按原意,作者的ID不是{1”,而是Column 'author_id' in where clause is ambiguous

我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

通过将模型中的方法更改为解决了该问题:

public function get_posts_by_author($authorid) {
    $this->db->select('posts.*,categories.name as post_category');
    $this->db->order_by('posts.id', 'DESC');
    $this->db->join('categories', 'posts.cat_id = categories.id', 'inner');
    // the line below was changed
   $query = $this->db->get_where('posts', array('posts.author_id' => $authorid));
    return $query->result();
}