Hello, 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.
If I run the query the old way it works perfectly:
$mysqli->$query("INSERT INTO table (id, field1) VALUES (0x" . $id . ",'" . $field1 . "')");
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:
if($stmt = $mysqli->prepare("INSERT INTO table (id, field1) VALUES (?, ?)")) {
$stmt->bind_param('ss', "0x".$id, $field1);
//execute statement
}
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:
if($stmt = $mysqli->prepare("INSERT INTO table (id, field1) VALUES (?, ?)")) {
$stmt->bind_param('bs', $id, $field1);
//execute statement
}
It gives no error, the row is inserted, but the identifier field now is empty (not null, empty).
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.