filed中必须是CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
连接时必须set names "utf8mb4";
这是一段PHP示例
<?php
//$emoji_1 = "?????2\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
//$emoji_1 = "zß\u6c34\U0001f34c";
$emoji_1 = "\xf0\x9f\x8d\x8c";
$dbh = new PDO( 'mysql:host=localhost;dbname=test', 'root', '' );
$dbh->exec(' set names "utf8mb4" ');
$tablename = "test_9"; if($_GET['show'] == 1){
$query = 'select * from `'.$tablename.'` where `id` = ?'; $stm = $dbh->prepare($query);
$stm->bindValue(1, $_GET['id'], PDO :: PARAM_INT );
$stm->execute();
while($row = $stm->fetch( PDO :: FETCH_ASSOC )){
var_dump($row);
}
}else{
$query = 'insert into `'.$tablename.'` (`name`, `a`)values(?, ?)';
var_dump(strlen($emoji_1));
$stm = $dbh->prepare($query);
$stm->bindParam(1, $emoji_1, PDO :: PARAM_STR, strlen($emoji_1));
$stm->bindValue(2, 3, PDO :: PARAM_INT );
if(!$stm -> execute ()){
var_dump($stm -> errorInfo ());
}else{
echo "okay.\n";
}
}
附:
MySql中可以用UNHEX(FF...)插入HEX值,HEX值不会经过处理直接,写入内容。例如插入一个UTF8字符,输入的是这个字符的UTF8编码,而不是unicode编码
INSERT INTO my_tbl (my_col) VALUES (UNHEX('F09F949F'));