title: PHP/mysqli: Inserting IP address with mysqli_stmt_bind_param()
body: <p>I have a database table which contains an unsigned integer field to store the visitor's IP address:</p>
  
  <pre><code>`user_ip` INT(10) UNSIGNED DEFAULT NULL,
  </code></pre>
  
  <p>Here's the snippet of PHP code which tries to store the IP address:</p>
  
  <pre><code>$ipaddr = $_SERVER['REMOTE_ADDR'];
  if ($stmt = mysqli_prepare($dbconn, 'INSERT INTO visitors(user_email, user_ip) VALUES (?,?)'))
  {
      $remote_ip = "INET_ATON('$ipaddr')";
      mysqli_stmt_bind_param($stmt, 'ss', $email, $remote_ip);
      if (mysqli_stmt_execute($stmt) === FALSE) return FALSE;
      $rows_affected = mysqli_stmt_affected_rows($stmt);
      mysqli_stmt_close($stmt);
  }
  </code></pre>
  
  <p>The INSERT operation succeeds, however the user_ip field contains a null value.
  I have also tried changing the parameter type in mysqli_stmt_bind_param() <em>(which was set to string in the above example)</em> to integer, i.e. mysqli_bind_param(... 'si',...) - but to no avail.</p>
  
  <p>I've also tried using the following bit of code instead of mysql's INET_ATON() SQL function:</p>
  
  <pre><code>function IP_ATON($ipaddr)
  {
      $trio = intval(substr($ipaddr,0,3));
      return ($trio&gt;127) ? ((ip2long($ipaddr) &amp; 0x7FFFFFFF) + 0x80000000) : ip2long($ipaddr);
  }
  </code></pre>
  
  <p>It still doesn't work - the 'user_ip' field is still set to null. I've tried passing the $ip_addr variable as both integer &amp; string in mysqli_bind_param() - to no avail.</p>
  
  <p>It seems the problem lies with the parameterized insert.</p>
  
  <p>The following "<em>old-style</em>" code works without any problem:</p>
  
  <pre><code>mysqli_query(..., "INSERT INTO visitors(user_email, user_ip) VALUES ('$email',INET_ATON('$ipaddr'))");
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p>Thanks in advance!</p>