I am trying to insert binary data into BLOB data type field on a MySql database and i have the following code:
$dataset = array();
if (!empty($_FILES)) {
foreach ($_FILES as $k => $f) {
$dataset[$k] = "data:" . $_FILES[$k]["type"] . ";" . file_get_contents($_FILES[$k]["tmp_name"]);
}
}
$sql = "UPDATE table_name SET ";
foreach ($dataset as $key => $val) {
if (!is_null($val) && $val !== false)
$sql .= $key . "=" . (is_numeric($val) ? $val : "'" . $val . "'") . ",";
}
}
$sql = rtrim($sql, ",");
$sql .= " WHERE id = 1";
$mysql = new mysqli("localhost" , "db_username", "db_password", "db_name");
$mysql->query($sql);
I already read about base64_encode, but i want to put the raw binary into database, so the mysql blob type will treat it like it should.
I also found about mysql_real_escape_string() function, but i saw another code working without escaping the binary data.
I know that is the attempt to send the raw binary to mysql that is making trouble, because mysql returns a 1064 error and when i remove the binary from the query, it simply works magically!
Can somebody help?
PS: Please, do not mark it as duplicate, because before asking here, i already did a good research on other threads, but all the answers i got did not work.