CodeIgniter ActiveRecord join() method wrapping numbers with backticks

吃可爱长大的小学妹 提交于 2019-12-06 05:12:59

You SHOULD not use the double quotes in SQL query:

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

Update:

This is a bug in the current CI stable version (fixed in v3.0-DEV), CI ActiveRecord methods (which doesn't implement really ActiveRecord) are prepared for simple usages.

I fixed this issue before by hacking the core files (by adding a parameter to join method to disable _protect_identifires).

There we go:

In system/database/DB_active_rec.php line #310, add $escape as 4th parameter:

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

And change $match[3] = ... to:

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

So, you can use join($table, $cond, $type = '', $escape = FALSE) to disable escaping.

In addition, setting _protect_identifires globally to FALSE is not in a correct direction.

the only option remains is using custom query():

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

Try this

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

or

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

Update:

Define

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

in "application/config/database.php" at the end.

try this one

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

please let me know if you face any problem.

Dont use $this->db->escape

$this->db->join('post_likes', 'post_likes.user_id="'.$online_user.'" AND post_likes.post_id=post.id', 'left');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!