Europeuser Europeuser - 5 months ago 9
PHP Question

Mysql query to check if url exists in database

lets say I have $_POST['url']='http://myexamplesite.com/images/image.jpg';
how can I get correct result if this url exists in the database but looks like:
http://www.myexamplesite.com/images/image.jpg

if I do:

$q='select id from products where url LIKE "%'.$_POST['url'].'%"';


This will not return always correct results. What is the correct way?
Thanks

Answer

If you need pure SQL solution, you can use SUBSTRING_INDEX() for that:

SELECT * FROM products WHERE
SUBSTRING_INDEX(SUBSTRING_INDEX(url, 'http://', -1), 'www.',-1) = 
SUBSTRING_INDEX(SUBSTRING_INDEX('".$_POST['url']."', 'http://', -1), 'www.',-1)

This will remove http:// and www. from both strings and will compare the rest.