【问题标题】:Migrating blob field mysqli迁移 blob 字段 mysqli
【发布时间】:2019-07-28 04:00:56
【问题描述】:

首先,抱歉我的英语不好,如果帖子得到回复,我很抱歉,但我没有找到。

我想“迁移”mysqli 中的blob 字段,因此,我想获取记录并将其存储在其他数据库中(具有多个连接)。

我有下一张桌子:

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);
}

【问题讨论】:

  • 您的INSERT 声明如何?你到底为什么要将文件保存在数据库中? “迁移”是什么意思?
  • @NicoHaase 我用代码编辑了帖子,感谢您的帮助!
  • 那么,您检查生成的查询了吗?它是否包含预期的信息?
  • 否,查询显示 blob 列为 NULL
  • 你检查过是否真的包含源表中的数据吗?

标签: php mysql mysqli migration blob


【解决方案1】:

我终于找到了解决方案。

我使用如下:

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

感谢您的回复!

【讨论】:

    猜你喜欢
    • 2022-06-14
    • 1970-01-01
    • 2015-06-03
    • 2012-11-26
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2020-12-28
    相关资源
    最近更新 更多