编码:
<?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,我 必须 设置character_set_client和character_set_results。
latin1
character_set_client
character_set_results
使用时SET CHARACTER SET utf8,出现了BLOB的问题。
SET CHARACTER SET utf8
但是当我使用SET NAMES utf8它时,它起作用了!
SET NAMES utf8