Questions And Answers

More Tutorials

MYSQL Connecting with UTF 8 Using Various Programming language


1st or 2nd line in source code (to have literals in the code utf8-encoded):

# -*- coding: utf-8 -*-


db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME,
 charset="utf8mb4", use_unicode=True)

For web pages, one of these:

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


In php.ini (this is the default after PHP 5.6):

default_charset UTF-8

When building a web page:

header('Content-type: text/plain; charset=UTF-8');

When connecting to MySQL:

(for mysql:) Do not use the mysql_* API!
(for mysqli:) $mysqli_obj->set_charset('utf8mb4');
(for PDO:) $db = new PDO('dblib:host=host;dbname=db;charset=utf8', $user, $pwd);

In code, do not use any conversion routines.

For data entry,

<form accept-charset="UTF-8">

For JSON, to avoid \uxxxx:

$t = json_encode($s, JSON_UNESCAPED_UNICODE);


In this page (written and validated by ) you learned about MYSQL Connecting with UTF 8 Using Various Programming language . What's Next? If you are interested in completing MYSQL tutorial, your next topic will be learning about: MYSQL Time with subsecond precision.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.