Current Location: Home> Latest Articles> SQL Query Garbled After mysqli::set_charset? How to Ensure Characters Are Transmitted Correctly

SQL Query Garbled After mysqli::set_charset? How to Ensure Characters Are Transmitted Correctly

gitbox 2025-09-20
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Irrelevant pre-code example</span></span><span>
</span><span><span class="hljs-variable">$pre_data</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(</span><span><span class="hljs-string">"apple"</span></span><span>, </span><span><span class="hljs-string">"banana"</span></span><span>, </span><span><span class="hljs-string">"cherry"</span></span><span>);
</span><span><span class="hljs-keyword">foreach</span></span><span> (</span><span><span class="hljs-variable">$pre_data</span></span><span> </span><span><span class="hljs-keyword">as</span></span><span> </span><span><span class="hljs-variable">$item</span></span><span>) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strtoupper</span></span><span>(</span><span><span class="hljs-variable">$item</span></span><span>) . </span><span><span class="hljs-string">"<br>"</span></span><span>;
}
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span><?php<br>
// Article body starts<br>
echo "<h1>SQL Query Garbled After mysqli::set_charset? How to Ensure Characters Are Transmitted Correctly</h1>";</p>
<p>echo <span><span class="hljs-string">"<p>When using PHP's <code>mysqli
";
echo "

Here, utf8mb4 is the recommended character set, as it supports full Unicode, including emojis.

"
;

echo "

3. Common Scenarios of Garbled Text and Solutions

"
;

echo "

Scenario 1: Table or Column Encoding Mismatch

"
;
echo "

Even if set_charset is set correctly, if the database table or columns are still latin1, Chinese or special characters will be decoded incorrectly.

"
;
echo "

Solution:

"
;
echo "
<br>
ALTER TABLE your_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<br>
"
;

echo "

Scenario 2: Page Display Encoding Mismatch

"
;
echo "

If the webpage uses UTF-8 encoding and MySQL data is utf8mb4, it usually displays correctly. But if the page is declared as ISO-8859-1 or not declared at all, Chinese characters will still appear garbled.

"
;
echo "

Ensure the page header includes:

"
;
echo "
<br>
<meta charset='UTF-8'><br>
"
;

echo "

Scenario 3: Encoding Mismatch Before Data Insertion

"
;
echo "

The character encoding from forms or APIs must match the MySQL connection encoding; otherwise, garbled text will appear during insertion.

"
;
echo "

Solution: Use a consistent encoding, e.g., both frontend and PHP should use UTF-8.

"
;

echo "

4. Best Practices

"
;
echo "
    ";
    echo "
  1. Use utf8mb4 as the character set for databases and tables.
  2. "
    ;
    echo "
  3. Immediately call mysqli::set_charset('utf8mb4') after connecting PHP to MySQL.
  4. "
    ;
    echo "
  5. Ensure the frontend webpage declares , and form submissions use UTF-8.
  6. "
    ;
    echo "
  7. Avoid manually using utf8_encode or utf8_decode; work directly with raw UTF-8 data.
  8. "
    ;
    echo "
"
;

echo "

Conclusion

"
;
echo "

Character corruption usually happens when the client, database, and display encodings are inconsistent. Using mysqli::set_charset is a key step to ensure proper communication between PHP and MySQL, but it must be combined with unified database and page encodings to fully eliminate garbled text issues.

"
;
?>

<?php // Irrelevant footer code example $footer_links = array("Home", "About", "Contact"); foreach ($footer_links as $link) { echo "" . ucfirst($link) . " | "; } ?>