Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting An IP Address To An Integer Using MySQL

Posted on 2013-06-15
9
Medium Priority
?
440 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 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

916 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