Migrating blob field mysqli Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Migrating blob field mysqli without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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

Answer

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!

We are here to answer your question about Migrating blob field mysqli - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji