PHP MySQL find IP address (between) stored IP address ranges

Hi,

I am wondering how to code a PHP MySQL 'SQL' query to find the given IP address between 2 stored IP address ranges.

I am trying this:
$theIp  = 'xxx.xxx.xxx.xxx';
$result = mysqli_query($link,"SELECT * FROM `ipAddresses` WHERE `start` <= '$theIp' AND `end` >= '$theIp'") or die(mysql_error());

But it doesn't seem to be working correctly.  Should i store the IP data differently? Not in one field.

Thanks in advance!
dev09Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quizwedgeCommented:
Check out the inet_aton MySQL function which will convert the IP address to an integer (https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton)

You should be able to do something like
$theIp  = 'xxx.xxx.xxx.xxx';
$result = mysqli_query($link,"SELECT * FROM `ipAddresses` WHERE inet_aton(`start`) <= inet_aton('$theIp') AND inet_aton(`end`) >= inet_aton('$theIp')") or die(mysql_error());

Open in new window

The syntax on that may be off as I didn't test it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
one of the methods is to convert the IP Address to "IP Numbers" and then do the respective comparison.

in php you can use ip2long function:

ip2long
http://php.net/manual/en/function.ip2long.php

in MySQL, use INET_ATON:
https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
ah, ok, think using INET_ATON is enough here, so no need to use php function ip2long.
Ray PaseurCommented:
This article shows how I've used IP numbers in PHP.  I used the MaxMind database to create a RESTful web service that could be used to identify the client country from its IP address.  Working code examples are included.
http://www.experts-exchange.com/articles/3437/IP-Address-to-Country-in-PHP.html

You might also want to look into the WordPress plugin called Akismet.  It's an anti-spam tool with IP awareness.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.