php - 代码点火器 3 : where clause gives an ambiguous column error

标签 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 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/

相关文章:

javascript - 在 jquery 中添加最后一个表时迭代 php 数组转换为 json

php - mysql语法使用IN求助!

php - PHP Codeigniter 中的重定向

php - 如何使用php通过图像名称调整文件夹中图像的大小?

java - JPA多对一: Deleting One to Many side

python - flask Python : Accept data from Ionic app into flask

php - 获取所有项目按特定列值分组

javascript - CodeIgniter - 使用表单辅助函数创建动态 html 表?

带有 ping 和 mysql 查询结果的 php 主机状态

php - Codeigniter - SQLServer 数据库连接错误