Solved

Converting An IP Address To An Integer Using MySQL

Posted on 2013-06-15
9
417 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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