Question

I'm inserting data into a log table, including an IP address. For some reason, the IP column is sometimes NULL'ed. In most cases (99.9% of all records) the IP is recorded properly.

The IPs get stored in an unsigned INT column, using the inet_aton() function. This translates an IP address from it's written dot-separated form (zzz.xxx.yyy.www) to its numeric form.

The MySQL documentation states the following regarding INET_ATON() and NULLs:

INET_ATON() returns NULL if it does not understand its argument.

INET_ATON() may or may not return a non-NULL result for short-form IP addresses (such as '127.1' as a representation of '127.0.0.1').

I've been logging the data flowing into the query, and have verified that the parameters being bound do in fact contain the IP address and not null. They IP addresses look fine, and if I manually run a

SELECT INET_ATON('zzz.yyy.xxx.www');

the result is a numeric representation, as expected.

For completeness, the code (note: removed seemingly irrelevant columns):

/* @var $conn PDO */
$stmt = $conn->prepare(
'INSERT INTO subscription_log(
    email, groupid, subscribe_ip
) VALUES (
    :email,
    :groupid,
    inet_aton(:label_subscribe_ip),
)');
$result = $stmt->execute($params);

// With the following input for $params (print_r):
Array
(
    [email] => user@domain.com
    [groupid] => 51299
    [label_subscribe_ip] =>  145.222.138.151
)

Finally, though I doubt it's relevant, MySQL version information:

version = 5.5.27-28.1
version_comment = Percona Server (GPL), Release 28.1
version_compile_machine = x86_64
version_compile_os = Linux

To me, it's mystifying. Any suggestions on how to go about further debugging this, anyone?

Was it helpful?

Solution

You probably have whitespace (or non printable character) added to your input.

Assuming you posted literal output here:

Array
(
    [email] => user@domain.com
    [groupid] => 51299
    [label_subscribe_ip] =>  145.222.138.151
)

You have two spaces before 145. meaning that the real input is _145.222.138.151 (with a space in the beginning).

Try using trim() before adding ip to parameters list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top