duanqiongchong0354 2016-03-23 11:25
浏览 56

如何拆分长数据并将其作为块在php mysql中发送?

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();
  • 写回答

1条回答 默认 最新

  • dsjpik057730 2016-03-23 11:29
    关注

    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.

    An image is a binary data all the same. So you can use any tutorial.

    To fix your code just get rid of that chunking stuff and run your query usual way.

    $stmt->bind_param("ssb", $eid, $ekey, $ecipher);
    $stmt->execute();
    

    IS ALL THE CODE YOU NEED

    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。