Utf8 in MySQL (and seen well in phpMyAdmin) but why not display correctly in web page (charset-utf8)?

StackOverflow https://stackoverflow.com/questions/18996530

  •  29-06-2022
  •  | 
  •  

Question

I have a table in mysql that utf8_turkish_ci encoded. Also the colomn has encoded as utf8_turkish_ci (varchar 250). But Turkish chars are not displayed properly. Manually written Turkish Characters in body displayed correctly. I checked the header. Page header is:

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Length:405
Content-Type:text/html; charset=utf-8
Date:Wed, 25 Sep 2013 04:50:39 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By:PHP/5.4.7

Turkish chars are: Üü İı Öö Ğğ Şş

Web page side

<?php 
header('Content-Type: text/html; charset=utf-8');
?>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php

I want to stay in utf8. How can i get data from MySQL and correctly display them in web page? I tried many of solution suggests out there :/

Was it helpful?

Solution

Make sure you are using set names utf8. If using PDO, you can do the following in your connection to the database

$db = new PDO($dsn, $user, $pass, [
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
]);

OTHER TIPS

put this line just above your query of selecting data.

mysql_query("SET NAMES 'utf8'");

hope this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top