Europeuser Europeuser - 1 year ago 85
PHP Question

Mysql query to check if url exists in database

lets say I have $_POST['url']='';
how can I get correct result if this url exists in the database but looks like:

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?

Answer Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download