【问题标题】:CodeIgniter ActiveRecord join() method wrapping numbers with backticksCodeIgniter ActiveRecord join() 方法用反引号包裹数字
【发布时间】:2013-08-13 11:27:36
【问题描述】:

这是我在 CodeIgniter 中的活动记录代码:

$this->db->...
$this->db->join('post_likes', 'post_likes.user_id="'.$this->db->escape($online_user).'" AND post_likes.post_id=post.id', 'left');

这就是它的解释方式:

LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id

它给出了错误:

`user_id`="`3"` 

如何在活动记录中写入直号?


更新:

删除escape

要在您的计算机上测试它,您不需要数据库。只是尝试此代码显示错误:

$this->db->select('*')
    ->from('mytable')
    ->join('post_likes', 'post_likes.user_id="3" AND  post_likes.post_id=post.id', 'left');
$query=$this->db->get('');
var_dump($this->db->last_query());
exit(0);

结果:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '` AND post_likes.post_id=post.id' at line 3

SELECT * FROM (`mytable`) LEFT JOIN `post_likes` ON `post_likes`.`user_id`="`3"` AND post_likes.post_id=post.id

【问题讨论】:

    标签: codeigniter activerecord codeigniter-2


    【解决方案1】:

    您不应该在 SQL 查询中使用双引号:

    $this->db->join('post_likes', "post_likes.user_id = $online_user AND post_likes.post_id=post.id", 'left');
    

    更新:

    这是当前 CI 稳定版本中的一个错误(在 v3.0-DEV 中修复),CI ActiveRecord 方法(实际上并未实现 ActiveRecord)是为简单的使用而准备的。

    我之前通过破解核心文件解决了这个问题(通过在加入方法中添加参数来禁用_protect_identifires)。

    我们去:

    system/database/DB_active_rec.php 第310 行,添加$escape 作为第4 个参数:

    public function join($table, $cond, $type = '', $escape = TRUE)
    

    并将$match[3] = ... 更改为:

    if ($escape === TRUE)
    {
        $match[3] = $this->_protect_identifiers($match[3]);
    }
    

    所以,你可以使用join($table, $cond, $type = '', $escape = FALSE) 来禁用转义

    此外,将_protect_identifires 全局设置为FALSE 方向不正确。

    剩下的唯一选择是使用自定义query()

    $sql = "SELECT * FROM some_table WHERE id = ?"
    $this->db->query($sql, array(3));
    

    【讨论】:

    • 如果帖子没有赞怎么办!那么它是否会从查询中显示该帖子? where 可能会省略结果。
    • @arumat 我没有参加你使用了“LEFT JOIN”。刚刚更新了答案。
    • 我在问题的末尾添加了一个工作台代码。您可以查看它并在测试环境中运行,看看它是如何为您呈现的。
    • @arumat 我没有你的数据库架构,试试我的解决方案,请告诉我它是如何工作的。
    • 您不需要创建架构。问题出在它在 db 上运行之前。
    【解决方案2】:

    试试这个

        $this->db->join('post_likes', "post_likes.user_id=$the_userid AND 
         post_likes.post_id=post.id", 'left');
    

     $this->db->join('post_likes', 'post_likes.user_id="'.$the_userid.'" AND 
             post_likes.post_id=post.id', 'left');
    

    更新:

    定义

    $db['default']['_protect_identifiers']= FALSE; 
    

    在“application/config/database.php”末尾。

    【讨论】:

    • 给出`user_id`=`3`怎么去掉`?
    • 这里 user_id 是整数类型,所以你不需要单引号('')。
    • 即使在没有'" 的情况下直接输入 3 也会换行`
    • @arumat 如果 user_id 的数据类型是 INT,第一个应该可以工作。
    • 即使使用$this->db->join('post_likes', 'post_likes.user_id=3 AND post_likes.post_id=post.id', 'left'); 将 3 渲染为“`3”`对我来说奇怪的是“和`没有考虑先进后出模式
    【解决方案3】:

    简单的解决方案是在连接查询之前暂时关闭protect_identifiers,如下所示:

    $this->db->_protect_identifiers = false;
    

    进行连接查询后,您可以将其设置回true

    在 CodeIgniter 2.1.2 版中为我工作

    【讨论】:

      【解决方案4】:

      试试这个

      $this->db->join('post_likes', 'post_likes.user_id="{$online_user}" AND post_likes.post_id=post.id', 'left');
      

      如果您遇到任何问题,请告诉我。

      【讨论】:

        【解决方案5】:

        不要使用$this->db->escape

        $this->db->join('post_likes', 'post_likes.user_id="'.$online_user.'" AND post_likes.post_id=post.id', 'left');
        

        【讨论】:

          猜你喜欢
          • 2022-01-12
          • 2011-12-13
          • 1970-01-01
          • 1970-01-01
          • 2019-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多