I have a table called Blocks
that looks like this:
| startIpNum | endIPNum | locId |
| int(11) | int(11) | int(11) |
Simple enough now. As you've guessed this is a list of IP ranges. I am using it for geolocation. Now I get the $_SERVER['REMOTE_ADDR']
and convert it to an integer and am trying to find the range the value falls between using this query
"SELECT * FROM `Blocks` WHERE startIPNum >= '$ip_addr' AND endIPNum <= '$ip_addr' "
where $ip_addr
is the integer form of REMOTE_ADDR
. Now the problem is that the query returns an empty result in both my PHP and MySQL in phpMyAdmin, even though I know there is a row in the table the IP number lies between as my IP number is 1677352691
and a row in the table is
| startIpNum | endIPNum | locId |
1677351936 | 1677354111 | 12431
So my conclusion is that my query is formed incorrectly. So how do I proper query the table to get the result I desire?
You're giving incorrect conditions,
SELECT * FROM `Blocks`
WHERE startIPNum <= $ip_addr
AND endIPNum >= $ip_addr
Try this without quotes, as they are of int
type, and let me know the case then -
EDIT-
Quotes wont matter in the case , curtsey peterm
SELECT *
FROM `Blocks`
WHERE startIPNum <= $ip_addr AND endIPNum >= $ip_addr "
You can also use BETWEEN
, like this,
one more thing you are setting $ip_addr
in both matching case
SELECT *
FROM `Blocks`
WHERE $ip_addr BETWEEN startIPNum AND endIPNum "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.