Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to query ip in p range

    I can't figure this. Can this be done alone with mysql, or there are somethings I need to do with php script.Let say I have many ip range stored in the database one of them is, 142.58.103.1 - 142.58.103.45. The table structure is like this

    Code:
        ip                                                 customer name
    142.58.103.1 - 142.58.103.45                                name1
    119.111.81.224 - 119.111.81.255                           name2
    Let say someone accessed a website and his ip is 142.58.103.12. Now with this ip, it should get name1 from customer name column as it's is in the range of 142.58.103.1 - 142.58.103.45.
    Last edited by Anishgiri; 05-20-2014 at 11:20 AM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,953
    Thanks
    2
    Thanked 172 Times in 167 Posts
    That's a very poor way to store the IP ranges. You should split that into 2 fields such as start_IP and end_IP. It would also be better to store them as integers rather than strings.

    Assuming you made those changes, then the select statement would be:
    Code:
    select customer_name from tablename where start_IP >= $IP and end_IP <= $IP;
    If you don't make those changes, then we might be able to work up a complex query that does what you want, but it wouldn't be very maintainable.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •