<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This example demonstrates using PHP mysqli::character_set_name to check the database connection charset</span></span><span>
</span><span><span class="hljs-comment">// Make sure you have configured the database connection parameters correctly</span></span><span>
<p></span>$mysqli = new mysqli("localhost", "username", "password", "database");</p>
<p>if ($mysqli->connect_error) {<br>
die("Connection failed: " . $mysqli->connect_error);<br>
}</p>
<p>// Set the expected charset<br>
$expectedCharset = 'utf8mb4';<br>
$mysqli->set_charset($expectedCharset);</p>
<p>// Get the charset currently used by the connection<br>
$currentCharset = $mysqli->character_set_name();</p>
<p>echo "Current charset: $currentCharset\n";</p>
<p>if ($currentCharset === $expectedCharset) {<br>
</span>echo "Charset matches, the application can safely operate on the data.\n";<br>
} else {<br>
echo "Charset mismatch, current is $currentCharset, expected $expectedCharset. Please check your settings.\n";<br>
}</p>
<p>$mysqli->close();<br>
</span>?></p>
<hr>
<p># How to Use mysqli::character_set_name to Verify if Database Charset Matches the Application Charset?</p>
<p>When developing PHP applications, charset issues are one of the common problems affecting correct data reading, writing, and display. Especially when handling multilingual content, inconsistent charsets may cause garbled text, data loss, or even security risks. Therefore, after establishing a MySQL database connection, verifying whether the charset in use matches the application's expected charset is a crucial step.</p>
<p><span><span class="hljs-comment">## What is <code>mysqli::character_set_name
By default, mysqli uses the latin1 charset, which often does not meet the needs of modern applications (especially Chinese applications). Therefore, we usually need to set the charset manually, for example, to utf8mb4.
<span><span><span class="hljs-variable">$mysqli</span></span>-><span><span class="hljs-title function_ invoke__">set_charset</span></span>("utf8mb4");
</span></span>
After setting, call character_set_name() again to confirm if the setting took effect.
In practice, it’s recommended to write some logic code to automatically check charset matching:
<span><span><span class="hljs-variable">$expectedCharset</span></span> = 'utf8mb4';
</span><span><span class="hljs-variable">$mysqli</span>->set_charset($expectedCharset);
<p></span>$currentCharset = $mysqli->character_set_name();</p>
<p>if ($currentCharset !== $expectedCharset) {<br>
// Log error or trigger warning<br>
error_log("Charset mismatch: current is $currentCharset, expected $expectedCharset");<br>
}<br>
Although MySQL provides `utf8`, it does not fully support the complete UTF-8 charset (only up to three-byte encoding), whereas `utf8mb4` supports the full UTF-8 set, including four-byte characters like emojis. Therefore, modern web applications are recommended to consistently use `utf8mb4`.
Using `mysqli::character_set_name()` is a simple and effective way to verify the actual charset used by the database connection. After setting the charset, verify it immediately to avoid issues caused by inconsistent encoding. Other components like database table definitions and HTTP headers should also consistently use the same charset to ensure data is stored and displayed correctly.
<span></span>
Related Tags:
mysqli