CodeCrack
CodeCrack

Reputation: 5363

Inserting IPv6 into database with codeigniter

my ip field in the mysql database is VARBINARY(16)

  // Insert into website table
    $ip = inet_pton('::1'); // Local ip
    $data_rec = array(
            'ip_address' => $ip,
            );  
    $this->db->insert('visitors', $data_rec); 

The ip doesn't get inserted. It's a blank field. Why is that?

Upvotes: 0

Views: 1380

Answers (1)

peterm
peterm

Reputation: 92785

inet_pton() returns unprintable characters.
Did you try to read it from the database and do echo inet_topn()?

Given that I have a table

CREATE TABLE `ip` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ip` varbinary(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

And using this simple script (all checks intentionally omitted for simplicity)

<?php
$in6_addr = inet_pton('::1');

$db = new mysqli('localhost', '******', '******', 'test');

$qry = "INSERT INTO ip (ip) VALUES('$in6_addr')";
$db->query($qry));

$qry = "SELECT * FROM ip WHERE id=1";
$result = $db->query($qry);
if ($row = $result->fetch_array(MYSQL_ASSOC)) {
    echo inet_ntop($row['ip']);
}    
?>

you get the output

::1

But you can't see the value in phpAdmin or Sequel Pro

Upvotes: 3

Related Questions