Solved

Converting An IP Address To An Integer Using MySQL

Posted on 2013-06-15
9
408 Views
Last Modified: 2013-06-17
Hello experts.
I'm trying to use this tutorial:
http://www.bennadel.com/index.cfm?event=blog.viewcode&id=1833&index=1
but i get an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @ipAsNumber = INET_ATON( @ipAddress ); SET @ipAsAddress = INET_NTOA(' at line 4

error line:
SET @ipAddress = '#ipAddress#';

I'm using local cf9 , mysql 5.5

Any help?
0
Comment
Question by:Panos
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 39250147
Is your DSN configured to allow multiple statements?  It's disabled by default to protect you against sql injection.  When it's disabled, that example would cause an error because it contains multiple SQL statements.
 
http://www.bennadel.com/blog/1542-MySQL-3-4-com-mysql-jdbc-Driver-And-allowMultiQueries-true.htm

Edit  But don't really need multiple statements. You could nest the functions instead:

      SELECT
      <cfqueryparam value="#ipAddress#" cfsqltype="cf_sql_varchar"> AS original
      , INET_ATON( <cfqueryparam value="#ipAddress#" cfsqltype="cf_sql_varchar"> ) AS ipAsNumber
      , INET_NTOA ( INET_ATON(<cfqueryparam value="#ipAddress#" cfsqltype="cf_sql_varchar">) ) AS ipAsAddress
       , CAST( INET_NTOA( INET_ATON(<cfqueryparam value="#ipAddress#" cfsqltype="cf_sql_varchar">) ) AS CHAR ) AS ipAsAddressString
0
 
LVL 2

Author Comment

by:Panos
ID: 39250529
Always something new. This is the exciting part. Thank you very much again.
I need your advise.
I read here http://cfsimplicity.com/63/saving-ip-addresses-as-integers-with-coldfusion-orm-and-mysql
about converting ip to ineger using orm . i tried the example and it is working for me
I never have use it until now. What is your opinion?
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 39250544
Thank you
regards
panos
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39250920
you may want to be aware that INET_ATON &  INET_NTOA are for IPv4 only

there are IPv6 equivalent functions (that deal with both IPv4 & IPv6)

INET6_ATON
https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-aton
INET6_NTOA
https://dev.mysql.com/doc/refman/5.6/en/miscellaneous-functions.html#function_inet6-ntoa
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:Panos
ID: 39250962
Hi PortletPaul
Thank you very much for your advise
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251000
seems I've been too late in just about everything today - but it would be a shame to work purely on IPv4 then discover you need to change for IPv6 some time later - particularly when this is avoidable.
0
 
LVL 2

Author Comment

by:Panos
ID: 39251098
Yes PortletPaul
You are absolutely right.
I'm very thankful for your post because without that i would go on with INET_ATON &  INET_NTOA.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39251111
:) happy coding
0
 
LVL 52

Expert Comment

by:_agx_
ID: 39252999
> there are IPv6 equivalent functions

Nice one, I didn't realize they'd added v6 functions!

@panosms - Just double check the max value. I'm not sure it always fits inside a "long"...

http://stackoverflow.com/questions/420680/how-to-store-ipv6-compatible-address-in-a-relational-database
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now