首页 文章

Codeigniter:MySQL Where子句和引号

提问于
浏览
1

Query in CodeIgniter

$this->db->select('comments.created_at, comments.section_id, comments.submittedby_id, users.username, comments.text, sections.name');
$this->db->order_by('comments.created_at', 'desc');
$this->db->where('comments.submittedby_id',  'users.user_id'); 
$this->db->where('comments.section_id', 'sections.id'); 

$query = $this->db->get(array('comments', 'users', 'sections'),10);

Produce SQL Request

SELECT pdb_comments.created_at,pdb_comments.section_id,pdb_comments.submittedby_id,pdb_users.username,pdb_comments.text,pdb_sections.name FROM(pdb_comments,pdb_users,pdb_sections)WHERE pdb_comments.submittedby_id ='users.user_id'AND pdb_comments.section_id ='sections .id'ORDER BY pdb_comments.created_at desc LIMIT 10

问题是 WHERE 子句中没有添加数据库前缀( pdb_ ) . 我可以通过附加 $this->db->dbprefix 手动插入前缀,但这不能解决主要问题 .

Quotes

`pdb_comments`.`submittedby_id` = 'pdb_users.user_id'

右侧的引号不准确,并为我生成0结果 . 有没有办法让CodeIgniter将where子句的后半部分识别为我的表的一部分;从而添加数据库前缀,并通过避免两个连接正确放置引号?还有另一种方法吗?提前致谢 .

乔恩

2 回答

  • 1

    使用:

    $this->db->select('comments.created_at, comments.section_id, comments.submittedby_id, users.username, comments.text, sections.name');
    $this->db->from('comments');
    $this->db->join('users', 'comments.submittedby_id=users.user_id'); 
    $this->db->join('sections', 'comments.section_id=sections.id'); 
    $this->db->order_by('comments.created_at', 'desc');
    $query = $this->db->get();
    

    代替 .

  • 3

    可能是一个愚蠢的问题,但为什么不直接编写SQL?界面看起来不像给你任何东西而是杂乱无章 .

相关问题