UTF8 in php and mysql
Posted by sarneil on 23 Nov 2016 in Activity log
Reminding myself of the 3 steps to ensure UTF gets from form via php to mysql database and back via php to report:
1) In each php page put the following meta tag in the head element
<meta charset="utf-8"/>
2) Immediately after creating the object that connects to the DB tell it to use UTF8
mysqli
$mysqliConn = new mysqli($DBHost, $DBUser, $DBPass, $DBDatabase);
$mysqliConn->query('SET NAMES utf8');
$mysqliConn->query('SET CHARACTER SET utf8');
PDO
$conn = new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8", $username, $password);
$conn->exec("set names utf8");
3) In the database (and each table) make sure the character encoding / collation is something like utf8_unicode_ci