迁移blob字段mysqli

问题描述 投票:0回答:1

首先,对不起我的英语不好,对不起,如果帖子回答,但我没有找到。

我想在blob中“迁移”mysqli字段,所以,我想get_records并将其存储在其他数据库中(具有多个连接)。

我有下一张桌子:

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

数据可以是文件(特别是图像,但不总是)..

应该如何在数据库中插入?

当我制作INSERT语句时,该列的值是null

一声问候!

编辑:这是我的代码:

    <?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);
}
php mysql mysqli migration blob
1个回答
0
投票

最后我找到了解决方案。

我用过如下:

    // $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;
    }

谢谢你的回复!

© www.soinside.com 2019 - 2024. All rights reserved.