0

I am trying to store ip addresses into my database and I am using INET_ATON as below,

$sql = "
INSERT INTO pin (
    page_id,
    user_id,
    pc_name,
    ip,
    geolocation,
    created_on
)VALUES(    
    ?,
    ?,
    ?,
    ?,
    ?,  
    NOW()
)";


$result = $this->connection->executeSQL($sql,array(
$this->page_id,
$this->user_id,
$this->getComputerName(),
'INET_ATON("123.136.106.104")',
$this->getGeoLocation()
));

But I can't get it converted or saved into the ip field in the db. I get 0 instead of number.

What should I do? What have I done incorrectly with the SQL?

Run
  • 54,938
  • 169
  • 450
  • 748
  • 1
    I suspect what you want to insert is `VALUES(?,?,?,?,INET_ATON(?),NOW())` and just pass the IP as a string parameter. – Joachim Isaksson Sep 30 '13 at 06:19
  • Use ip2long() and long2ip(). Follow this post for refrence [http://stackoverflow.com/questions/2754340/inet-aton-and-inet-ntoa-in-php][1] [1]: http://stackoverflow.com/questions/2754340/inet-aton-and-inet-ntoa-in-php – user2830076 Sep 30 '13 at 06:30

1 Answers1

1

You problem is not in INET_ATON function - but in how PDO (I assume it is PDO) interprets bound parameter. You can act like this instead:

$sql = "
INSERT INTO pin (
    page_id,
    user_id,
    pc_name,
    ip,
    geolocation,
    created_on
)VALUES(    
    ?,
    ?,
    INET_ATON(?),
    ?,
    ?,  
    NOW()
)";

and pass your IP:

$result = $this->connection->executeSQL($sql,array(
$this->page_id,
$this->user_id,
$this->getComputerName(),
"123.136.106.104",
$this->getGeoLocation()
));
Alma Do
  • 37,009
  • 9
  • 76
  • 105