Part of the EllisLab Network
   
 
Database Class: $this->db->like($field, $value, ‘both’); with Wildcards (Underscore ‘_’)
Posted: 09 February 2010 08:12 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  38
Joined  08-19-2008

Hey everyone,


I’m using the Active Record Class to get data from my DB (MySQL).
I’ve set up a form where a User can submit a search query which will call a Model with this line of code:

$this->db->like($field, $value, 'both');

Everything works fine, but now, I my client wants to submit Wildcards (especially the underscore) as well.

E.g. if the DB contains: 
‘aa1aa’, ‘aa2aa’, ‘aa3aa’, aaXbc’

$this->db->like($field, 'aa_', 'both');

Should return every four rows. But because of using DB’s like() method, the underscore is escaped, so the query is

WHERE field LIKE "ª\_%"

But I want it to be like this:

WHERE field LIKE "ª_%"

(without the escaped wildcard)


Is there a way to tell CI, it shouldn’t escape underscores?


Thanks in advance!

 Signature 

greets,
Toby Sommer

Profile
 
 
Posted: 09 February 2010 07:30 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  365
Joined  11-17-2008

I don’t think there is a real good way around it without extending the DB class to add in a “protect submission” boolean.
The mysql_driver class contains a “escape_str” method that is used by the DB class method. The escape_str method uses php’s native functions for
                 
1. ‘mysql_real_escape_string’
2. ‘mysql_escape_string’
3. ‘addslashes’
               
in that order depending on what is available. So really, it’s not the DB class Like method doing the escaping. In order to get around it, you would have to extend the mysql_driver class to accept a boolean as to whether or not you want to protect the user submitted value.
                         
NOTES:
* The escape_str method can be found in system/database/drivers/mysql/mysql_driver.php starting on line 285.
* The call to this method can be found in system/database/drivers/DB_driver.php, method escape_like_string line 705
* The call to escape_like_string method can be found in system/database/drivers/DB_active_record.php method _like line 697
* The call to _like method can be found in system/database/drivers/DB_active_record.php method like line 613

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120447 Total Logged-in Users: 30
Total Topics: 126531 Total Anonymous Users: 3
Total Replies: 665334 Total Guests: 364
Total Posts: 791865    
Members ( View Memberlist )