CodeIgniter Forums
Active Record : LIKE without wildcard (%) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record : LIKE without wildcard (%) (/showthread.php?tid=48797)



Active Record : LIKE without wildcard (%) - El Forum - 01-29-2012

[eluser]HattoriHanzo[/eluser]
You have mentioned in change log and new user guide that new capability of using "LIKE" without wildcard (%) has been added to the new version (2.1.0) :
in this link :
http://ellislab.com/codeigniter/user-guide/changelog.html
"Added additional option 'none' for the optional third argument for $this->db->like() in the Database driver."

But it seems you forgot to include it in uploaded files!!!!
I added this code segment to "DB_active_rec.php" file @ line 672 :

Code:
elseif ($side == 'none')
   {
    $like_statement = $prefix." $k $not LIKE '{$v}'";
   }



Active Record : LIKE without wildcard (%) - El Forum - 01-29-2012

[eluser]pickupman[/eluser]
This code was added back on 8/21 see the [url="https://github.com/EllisLab/CodeIgniter/commit/ea4ad9b4e7ab7824da4274e30075fd36a6bf9952"]commit[/url]. It was add the week after 2.1.0 was tagged and released.


Active Record : LIKE without wildcard (%) - El Forum - 01-29-2012

[eluser]HattoriHanzo[/eluser]
[quote author="pickupman" date="1327866015"]This code was added back on 8/21 see the [url="https://github.com/EllisLab/CodeIgniter/commit/ea4ad9b4e7ab7824da4274e30075fd36a6bf9952"]commit[/url]. It was add the week after 2.1.0 was tagged and released.[/quote]

I've just downloaded it again and there is no change!!!!
here is download link, check it yourself. Big Grin

Official Download Link From Home Page :
codeigniter[.]com/download_files/reactor/CodeIgniter_2.1.0.zip


Active Record : LIKE without wildcard (%) - El Forum - 01-30-2012

[eluser]pickupman[/eluser]
If you notice, I mentioned is was added <b>after</b> 2.1.0 was tagged and released. The next version has not been released, so when you download from the homepage you are still getting 2.1.0 and not the latest. Try clicking on the commit link in my message above and you will see the code is in fact there.


Active Record : LIKE without wildcard (%) - El Forum - 01-30-2012

[eluser]HattoriHanzo[/eluser]
[quote author="pickupman" date="1327931427"]If you notice, I mentioned is was added <b>after</b> 2.1.0 was tagged and released. The next version has not been released, so when you download from the homepage you are still getting 2.1.0 and not the latest. Try clicking on the commit link in my message above and you will see the code is in fact there.[/quote]

Oh, sorry. My poor English :-"


Active Record : LIKE without wildcard (%) - El Forum - 01-30-2012

[eluser]Aken[/eluser]
Code:
$this->db->like('column', 'value', 'none');
Is identical to:
Code:
$this->db->where('column', 'value');
At least in MySQL it is. I'd recommend using a direct comparison if you don't need the benefits the LIKE command gives you (such as the percent characters).


Active Record : LIKE without wildcard (%) - El Forum - 01-30-2012

[eluser]HattoriHanzo[/eluser]
[quote author="Aken" date="1327970940"]
Code:
$this->db->like('column', 'value', 'none');
Is identical to:
Code:
$this->db->where('column', 'value');
At least in MySQL it is. I'd recommend using a direct comparison if you don't need the benefits the LIKE command gives you (such as the percent characters).[/quote]

If you want to compare an Integer value like 'id' in your query and your input value is something like this ("2sad2s") , using "WHERE" in Active Records, create a query like this :

SELECT * FROM my_table WHERE id = "2sad2s" ;

As a result, MYSQL convert the string to an integer ("2" in this case) and run the query and return record corresponding to that row. But this is a semantic error.

The result is equivalent to this query :

SELECT * FROM my_table WHERE id = "2" ;

Instead of "WHERE", When you use "LIKE", there is no change in your input value.

SELECT * FROM my_table WHERE id LIKE "2sad2s" ;

Of course another solution is getting intval() of input value.


Active Record : LIKE without wildcard (%) - El Forum - 01-31-2012

[eluser]Aken[/eluser]
Hm, I've never ran into that before. Interesting.

In that case, I suggest that you validate the input before using it as a comparison in your database. If you know you're going to compare an integer field in your database, your PHP should make sure the value is an integer before it gets anywhere near the DB. I wouldn't recommend intval() either, since it'll do the same thing that MySQL does in that it'll turn a string like "2sad2s" into int(2). You want to validate directly against user input, not what PHP might think it should be.


Active Record : LIKE without wildcard (%) - El Forum - 01-31-2012

[eluser]HattoriHanzo[/eluser]
[quote author="Aken" date="1327993726"]Hm, I've never ran into that before. Interesting.

In that case, I suggest that you validate the input before using it as a comparison in your database. If you know you're going to compare an integer field in your database, your PHP should make sure the value is an integer before it gets anywhere near the DB. I wouldn't recommend intval() either, since it'll do the same thing that MySQL does in that it'll turn a string like "2sad2s" into int(2). You want to validate directly against user input, not what PHP might think it should be.[/quote]

I validate my Integer inputs like this :

Code:
if(strcmp(intval($input),$input)!=0)
  return FALSE;



Active Record : LIKE without wildcard (%) - El Forum - 01-31-2012

[eluser]danmontgomery[/eluser]
[quote author="Aken" date="1327970940"]
Code:
$this->db->like('column', 'value', 'none');
Is identical to:
Code:
$this->db->where('column', 'value');
At least in MySQL it is. I'd recommend using a direct comparison if you don't need the benefits the LIKE command gives you (such as the percent characters).[/quote]

LIKE is case insensitive, WHERE is not