Getting weird query string from Codigniter's LIKE()

强颜欢笑 提交于 2019-12-24 12:54:43

问题


I'm trying to make a simple query in Codigniter like this:

$this->db->select("cats");
$this->db->from("pets");
$this->db->like("name", 'a', 'after');

And then show it:

echo $this->db->get_compiled_select();

Result:

SELECT cats FROM pets WHERE name LIKE 'a%' {escape '!'}

For some reason CI adds {escape '!'} at the end. Which of course generates an error when I try to run the query.

Database config:

$db['pyramid'] = array(
'dsn'   => 'pyramid',
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'odbc',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'WINDOWS-1252',
'dbcollat' => '',
    //'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

Does anyone know how to fix this?

EDIT:

It works if I do it like this:

$this->db->select("cats");
$this->db->from("pets");
$this->db->where("name LIKE 'a%'", NULL, FALSE);

But then I'll have to escape the data myself.

SECOND EDIT:

It seems like CodeIgniter produces the correct query according to this.

Maybe it's because it's a Pervasive database.

I've creted a new thread with a question on how to get around the problem here instead.


回答1:


Use below query:-

$this->db->select('cats')
$this->db->from('pets');
$this->db->like('name', 'a', 'after');
$result = $this->db->get()->result_array();

You can also write it by below way:-

$result = $this->db->select('cats')->from('pets')
       ->where("name LIKE a%")->get()->result_array();

Hope it will help you :)




回答2:


Try

$var = $this->db->select("cats")->like("name", 'a', 'after')->get("pets")->result();
print_r($var);

Go to 363 of odbc driver and find or search with _like_escape_str you will definitely find something...

$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr);

and replace it with

$sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%' ";//or return false as needs 

maybe this can help



来源:https://stackoverflow.com/questions/35407012/getting-weird-query-string-from-codigniters-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!