title: PHP- inserting binary data in mysql using prepared statements
body: <p>I have to insert a row using php's mysql improved library to a table in mysql that has its primary key of type VARBINARY.
  The contents of this field are a computed sha1 hash.</p>
  
  <p>If I run the query the old way it works perfectly:</p>
  
  <pre><code>$mysqli-&gt;$query("INSERT INTO table (id, field1) VALUES (0x" . $id . ",'" . $field1 . "')");
  </code></pre>
  
  <p>But when I try to execute it as a prepared statement, I can't figure out how to do it. If 
  I perform the equivalent action:</p>
  
  <pre><code>if($stmt = $mysqli-&gt;prepare("INSERT INTO table (id, field1) VALUES (?, ?)")) {
      $stmt-&gt;bind_param('ss', "0x".$id, $field1);
      //execute statement
  }
  </code></pre>
  
  <p>It throws an exception saying that the contents were too large for this field. And If I try to insert it as a BLOB field:</p>
  
  <pre><code>if($stmt = $mysqli-&gt;prepare("INSERT INTO table (id, field1) VALUES (?, ?)")) {
      $stmt-&gt;bind_param('bs', $id, $field1);
      //execute statement
  }
  </code></pre>
  
  <p>It gives no error, the row is inserted, but the identifier field now is empty (not null, empty).</p>
  
  <p>I know I can mix the query and input the id concatenated in the string and the other fields as bind parameters of the prepared statement, but i'm asking just to know what is the correct way to insert this and perhaps it will help somebody in the future.</p>