简体   繁体   English

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

[英]Codeigniter 3: where clause gives an ambiguous column error

I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4. 我正在Codeigniter 3.1.8和Bootstrap 4中开发一个基本的博客应用程序

There are several tables, among which authors , posts and categories which stores the posts categories. 有几个表,其中authorspostscategories用来存储职位类别。

I am displaying the posts with the name of the category each post belongs to. 我正在显示带有每个帖子所属类别名称的帖子。

I also want to display post filtered by author. 我也想显示按作者过滤的帖子。 In other words, all posts written by an author . 换句话说, 所有作者撰写的帖子 For this purpose I have created a method in the Posts_model model: 为此,我在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();
}

In the Posts controller, I have the method: 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;
}

At /posts/byauthor/1 , instead of "1", the author's id, as it was intended, I see Column 'author_id' in where clause is ambiguous . /posts/byauthor/1 ,按原意,作者的ID不是“ 1”,而是Column 'author_id' in where clause is ambiguous看到了Column 'author_id' in where clause is ambiguous

Where is my mistake? 我的错误在哪里?

Solved the problem by changing the method in the model to: 通过将模型中的方法更改为解决以下问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM