我正在 Codeigniter 3.1.8 和 Bootstrap 4 中开发一个基本的博客应用程序。
有几个表,其中authors
、posts
和categories
存储帖子类别。
我正在显示帖子以及每个帖子所属类别的名称。
我还想显示按作者过滤的帖子。换句话说,由作者撰写的所有帖子。为此,我在 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
Controller 中,我有以下方法:
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
中,我看到 where 子句中的列“author_id”不明确
.
我的错误在哪里?
最佳答案
通过将模型中的方法更改为以下方式解决了问题:
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();
}
关于php - 代码点火器 3 : where clause gives an ambiguous column error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54268562/