I am using php to store data into mysql using prepared statements, and the data I am sending is in blobs, these are just cipher text encrypted with AES-256
, so I am storing them in a binary format so, it's not an image. I figured out in order to store blob data into mysql using php I need to use send-long-data
from mysqli
.
Since it's not an image, I couldn't find a single tutorial to store blob data which is not a file, and I am stuck.
I have prepared a code that splits the string into chunks, but the results seem to be incomplete and only the first chunk is stored, how do I store it correctly? I think the problem is somewhere in str_split()
, I would be grateful if you could correct the code. And also, I copied the chunk size from php.net. What should be the optimal size, in order to get the best performance, based on the fact that the cipher data maybe really big?
$stmt = $conn->prepare("INSERT INTO data_store (eid, ekey, ecipher) VALUES (?, ?, ?)");
//Initialise variables
$eid = $postedID;
$ekey = NULL;
$ecipher = NULL;
if($stmt->bind_param("sbb", $eid, $ekey, $ecipher)) {
// Send blobs
$ekey_chunks = str_split($postedKey, 8192);
foreach ($ekey_chunks as $chunk) {
$stmt->send_long_data(1, $chunk);
}
$ecipher_chunks = str_split($postedCipher, 8192);
foreach ($ecipher_chunks as $chunk) {
$stmt->send_long_data(2, $chunk);
}
$stmt->execute();
$stmt->close();
}
$conn->close();