gpt4 book ai didi

php - 迁移 blob 字段 mysqli

转载 作者:行者123 更新时间:2023-11-29 16:13:44 26 4
gpt4 key购买 nike

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

我想“迁移”mysqli中的blob字段,因此,我想获取_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);
}

最佳答案

终于找到解决办法了。

我使用如下:

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

感谢您的回复!

关于php - 迁移 blob 字段 mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55023931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com