Click to See Complete Forum and Search --> : MySQL WHERE clause using LIKE


devel95
07-02-2008, 12:32 PM
I have a column in a table that contains domain names. I want to use the $_SERVER['HTTP_HOST'] value to see if the domain name exists in my table. If one of the rows has 'abcdomain.com' in the domain name column, how do I code the WHERE LIKE clause to check for true against 'abcdomain.com', or 'www.abcdomain.com', or test.abcdomain.com', etc.?

My current PHP select statement looks like the following and is only returning true on the 'abcdomain.com':

SELECT * FROM myTable WHERE domainName LIKE '%".strtolower($_SERVER['HTTP_HOST'])."' LIMIT 1


Note: I recognize that the problem is my "haystack" string is smaller than the "needle" string in some instances. So I need to figure a way to handle that or otherwise swap the clause to something like this:

'%".strtolower($_SERVER['HTTP_HOST'])."' LIKE domainName


Thank you for your help.

devel95
07-02-2008, 02:20 PM
Well...spent more time on it and solved it this way:

mysql_query("SELECT * FROM myTable WHERE domainName=right('".strtolower($_SERVER['HTTP_HOST'])."',length(domainName)) LIMIT 1")

gabriele
07-02-2008, 04:27 PM
you query would return results also in the case where the domainName value would be main.com and the HTTP_HOST is any_name_ending_in_main.com

devel95
07-02-2008, 04:42 PM
My solution produces the correct results for all domain name structures. The first shot at it does not work that way.