re: Use of SQL INET_NTOA in joins

re: Use of SQL INET_NTOA in joins

ewmantheiewmanthei Posts: 14Questions: 1Answers: 2

This is in reference to:
https://datatables.net/forums/discussion/24021/use-of-sql-inet-ntoa-in-joins

The method Allan suggested works well for IPv4 but can't handle IPv6. The formatter below will take care of IPv4 or IPv6 and display them both correctly. Note, I'm storing the binaries in my database with the MySQL function inet6_aton('ip_address'). The MySQL function to pull them back out into a readable form is inet6_ntoa('binary').

Field::inst('ip_address')->getFormatter(function($ip){
    $l = strlen($ip);
    if ($l == 4 or $l == 16) {
        return inet_ntop(pack('A' . $l, $ip));
    }
    return '';
})

This discussion has been closed.