?
Solved

Converting An IP Address To An Integer Using MySQL

Posted on 2013-06-15
9
Medium Priority
?
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 49

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 49

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 49

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

719 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