Is BLOB converted using current / default encoding in MySQL?

  • I have a table with a BLOB field.
  • Shell of the Latin1 table.
  • I connect to the DB and "SET CHARACTER SET utf8".
  • Then I save the binary data in the field.
  • Then I retrieve the data, and that is not what I saved (corrupted).

The code:

<?php
$pdo = new \PDO("mysql:host=127.0.0.1;dbname=***", '***', '***');

$pdo->exec('SET CHARACTER SET utf8');

$sql = "INSERT INTO pdo_blob (the_blob) VALUES(:the_blob)";
$insertStm = $pdo->prepare($sql);

$blob = (binary) file_get_contents('/home/***/test.pdf');
$insertStm->bindParam(":the_blob", $blob, \PDO::PARAM_LOB);
$insertStm->execute();

$selectStm = $pdo->prepare("SELECT the_blob FROM pdo_blob ORDER BY id DESC LIMIT 1");
$selectStm->execute();

$savedBlob = null;
$selectStm->bindColumn(1, $savedBlob, \PDO::PARAM_LOB);
$selectStm->fetch();

echo 'equal: ' . ((int) ($blob == $savedBlob));
+5
source share
3 answers

Good answer @mvp!

But when my UTF-8 web application and database encoding latin1, I have to install character_set_clientandcharacter_set_results .

When I use SET CHARACTER SET utf8, I got the described blob issue.

But when I use SET NAMES utf8, it works!

+1
source

Short answer:

, , (utf8, latin1 ..):

$pdo->exec('SET CHARACTER SET utf8');

:

PDO, MySQL.

latin1, :

SET CHARACTER SET utf8

( : utf8, latin1 - , ), , , MySQL ( BLOB!).

SET CHARACTER SET, , (PHP/PDO Perl/DBI) , , .

, - , BLOB, , - .

PHP/PDO, Perl/DBI, : , latin1 SET CHARACTER SET utf8 ( ).

utf8, , :

ALTER DATABASE mydb CHARSET utf8;

utf8, BLOB .

, , - blob.bin 0xFF. Linux printf:

printf "0xFF" > blob.bin

, , :

PHP:

<?php
$dbh = new PDO("mysql:host=127.0.0.1;dbname=test");
# If database encoding is NOT utf8, uncomment to break it:
# $dbh->exec("SET CHARACTER SET utf8");

$blob1 = file_get_contents("blob.bin");
$sth = $dbh->prepare(
    "INSERT INTO pdo_blob (the_blob) VALUES(:the_blob)"
);
$sth->bindParam(":the_blob", $blob1, PDO::PARAM_LOB);
$sth->execute();

$sth = $dbh->prepare(
    "SELECT the_blob FROM pdo_blob ORDER BY id DESC LIMIT 1"
);
$sth->execute();

$blob2 = null;
$sth->bindColumn(1, $blob2, PDO::PARAM_LOB);
$sth->fetch();

if ($blob1 == $blob2) {
    echo "Equal\n";
} else {
    echo "Not equal\n";
    $arr1 = str_split($blob1);
    $arr2 = str_split($blob2);
    $i=0;
    for ($i=0; $i<count($arr1); $i++) {
        if ($arr1[$i] != $arr2[$i]) {
            echo "First diff: " . dechex(ord($arr1[$i])) . " != "
                                . dechex(ord($arr2[$i])) . "\n";
            break;
        }
    }
}
?>

Perl:

#!/usr/bin/perl -w

use strict;
use DBI qw(:sql_types);

my $dbh = DBI->connect("dbi:mysql:host=127.0.0.1;dbname=test");
# If database encoding is NOT utf8, uncomment to break it:
# $dbh->do("SET CHARACTER SET utf8");
open FILE, "blob.bin";
binmode FILE;
read(FILE, my $blob1, 100000000);
close FILE;
my $sth = $dbh->prepare(
    "INSERT INTO pdo_blob (the_blob) VALUES(?)"
);
$sth->bind_param(1, $blob1, SQL_BLOB);
$sth->execute();
my ($blob2) = $dbh->selectrow_array(
    "SELECT the_blob FROM pdo_blob ORDER BY id DESC LIMIT 1"
);
print ($blob1 eq $blob2 ? "Equal" : "Not equal") , "\n";
+5

Edit: on a WAMP server

It did not work with PDO API. You can use base64_encode()before insertion and base64_decode()after extraction. It inflates the data with 33%, and the conversion is overhead.

If the MySQLi API is an option, then here is some code:

<?php
$mysqli = new mysqli('localhost', 'spark', 'spark123', 'test');

$sql = "INSERT INTO blob_tb (bdata) VALUES(?)";
$insertStm = $mysqli->prepare($sql);

$blob = NULL; //necessary
$insertStm->bind_param('b', $blob);

$blob = (binary) (file_get_contents('favicon.ico'));
$insertStm->send_long_data(0, $blob);

$insertStm->execute();
$insertStm->close();

$selectStm = $mysqli->prepare("SELECT bdata FROM blob_tb LIMIT 1");
$selectStm->execute();

$selectStm->bind_result($savedBlob);
$selectStm->fetch();
$selectStm->close();

$mysqli->close();

echo 'equal: ' . ((int) ($blob == $savedBlob));
// var_dump(($blob), strlen($blob));
// var_dump(($savedBlob), strlen($savedBlob));
// var_dump(get_defined_vars());

?>
+1
source

All Articles