问题
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