是否使用MySQL中的当前/默认字符集转换了BLOB? [英] Is a BLOB converted using the current/default charset in MySQL?

查看:65
本文介绍了是否使用MySQL中的当前/默认字符集转换了BLOB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我有一个带有BLOB字段的表.
  2. 表的字符集为Latin1.
  3. 我连接到数据库和"SET CHARACTER SET utf8".
  4. 然后我将二进制数据保存到该字段中.
  5. 然后我检索数据,这不是我保存(损坏)的数据.

代码:

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

推荐答案

好答案@mvp!

但是,当我的Web应用程序为UTF-8并且数据库编码为latin1时,我必须

But when my web app is UTF-8 and the database encoding is latin1, I have to set the character_set_client and character_set_results.

当我使用SET CHARACTER SET utf8时,遇到了BLOB的问题.

When I use SET CHARACTER SET utf8, I got the described problem with BLOBs.

但是当我使用SET NAMES utf8时,它可以工作!

But when I use SET NAMES utf8 instead it works!

这篇关于是否使用MySQL中的当前/默认字符集转换了BLOB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆