将字符集(例如,utf8mb4)和排序规则与模式(数据库)设置对齐
<?php
$mysqli = new mysqli( DB_HOST, DB_USER, DB_PASSWORD, DB_SCHEMA, DB_PORT );
if ( 0 !== $mysqli->connect_errno )
throw new \Exception( $mysqli->connect_error, $mysqli->connect_errno );
if ( TRUE !== $mysqli->set_charset( 'utf8mb4' ) )
throw new \Exception( $mysql->error, $mysqli->errno );
if ( TRUE !== $mysqli->query( 'SET collation_connection = @@collation_database;' ) )
throw new \Exception( $mysql->error, $mysqli->errno );
?>
确认
<?php
echo 'character_set_name: ', $mysqli->character_set_name(), '<br />', PHP_EOL;
foreach( $mysqli->query( "SHOW VARIABLES LIKE '%_connection';" )->fetch_all() as $setting )
echo $setting[0], ': ', $setting[1], '<br />', PHP_EOL;
?>
将输出类似以下内容
character_set_name: utf8mb4
character_set_connection: utf8mb4
collation_connection: utf8mb4_unicode_520_ci