dongweng6241 2019-03-06 13:11
浏览 157
已采纳

迁移blob字段mysqli

first of all, sorry for my bad english, and sorry if post answered, but I don't found it.

I want to "migrate" blob field in mysqli, so, I want to get_records and store it in other database (with multiple connections).

I have the next table:

id | name | data | timecreated
1  | Foo  | BLOB | 1551863583

data could be a file(specially image, but not always)..

How should the insert in the database?

When I make the INSERT statement the value of that column is null.

A greeting!

EDIT: This is my code:

    <?php


$con1 = mysqli_connect($con1_host, $con1_user, $con1_pass, $con1_bbdd);
$con2 = mysqli_connect($con2_host, $con2_user, $con2_pass, $con2_bbdd);

function get_records_sql($sql, $con) {
    $ret = array();
    if ($results = $con->query($sql)) {
        while ($row = $results->fetch_array()) {
            $ret []= $row;
        }
        $results->close();
    }
    return $ret;
}

// Get records from foo_bar connection1
$records = get_records_sql("SELECT * FROM foo_bar", $con1);

foreach ($records as $record) {
    // Inserting records to foo_bar connection2
    $sql = "INSERT INTO foo_bar (`name `, `data`, `timecreated`) VALUES ('{$record['name']}', '{$record['data']}', '{$record['timecreated']}')";
    mysqli_query($con2, $sql);
    $id = mysqli_insert_id($con);
}
  • 写回答

1条回答 默认 最新

  • dqdt45183 2019-03-20 13:47
    关注

    Finally I found the solution.

    I used as follow:

        // $r is an array, and have data record from con1.foo_bar
    
        $sql = "INSERT INTO foo_bar (`id`, `name`, `data`, `timecreated`) VALUES ('{$r['id']}', '{$r['name']}', ?, '{$r['timecreated']}')";
    
        if ($stmt = $con->prepare($sql)) {
            $stmt->bind_param("b", $data);
            $stmt->send_long_data(0, $r['data']);
    
            if ($stmt->execute()) {
                $id = $stmt->insert_id;
            }
        } else {
            $error = $con->errno . ' ' . $con->error;
            echo $error;
        }
    

    Thanks for your replies!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元