Q:为什么要用utf8mb4
A:utf8mb4可以直接保存emoji字符
MySQL修改字符集
// 数据表编码改为utf8mb4
function to_utf8mb4()
{
$database = config("database.database");
$sql = "ALTER DATABASE `{$database}` CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`;"; //数据库
// Db::execute($sql);
echo $sql;
$lists = Db::getTables();
foreach ($lists as $key => &$value) {
Db::execute("ALTER TABLE `{$value}` CONVERT TO CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci`;"); //数据表
$fields = Db::getFields($value);
foreach ($fields as $key1 => &$value1) {
if (mb_strpos(strtolower($value1["type"]), "char") !== false) {
Db::execute("ALTER TABLE `{$value}` CHANGE `{$value1["name"]}` `{$value1["name"]}` {$value1["type"]} CHARACTER SET `utf8mb4` COLLATE `utf8mb4_general_ci` DEFAULT '{$value1["default"]}';"); //字段
}
}
}
}
thinkPHP5.0框架
// 数据库编码默认采用utf8
'charset' => 'utf8',
新的
// 数据库编码默认采用utf8
'charset' => 'utf8mb4',
修改之后可以直接存储emoji
普通的utf8字符
emoji
参考文章
https://blog.csdn.net/qq_50573146/article/details/125177260