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

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像