• 0

UTF8 PHP MySQL


Question

hey everyone,

 

 

I am using turkish within a MySQL table (utf8) it stores fine within the mysql database but when pushing it to the front end it displays as a question mark (a normal question mark NOT a black diamond)... 

 

I did have the black diamond issue as well but that is resolved but i cannot seem to fix this current issue for example here is a word where the g like character displays as a ?....Ma?aza = Ma?aza

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

Is your front end being encoded as UTF8 or...?

Link to comment
Share on other sites

  • 0

Could it be this?

 

http://php.net/manual/en/mysqli.set-charset.php

 

Maybe the mysql (or mysqli in this case) driver for PHP doesn't pull your data in UTF-8, so it ends up like this. I am not confident it is the answer, total shot in the dark here... But it might be something to try (if you coded your frontend yourself that is).

I am using PDO maybe I should have mentioned that (PDO FTW)

Link to comment
Share on other sites

  • 0

You can try specifying the charset that PDO uses in your connection string to see if that helps:

<?php $link = new PDO("mysql:host=localhost;dbname=DB;charset=UTF8"); ?>

(From http://php.net/manual/en/pdo.construct.php#113498. If your PHP version is < 5.3.6, you'll need to look at: http://stackoverflow.com/a/4361485/4996362)

 

I'm also not sure if your <meta> tag is formatted correctly or if it's just a format I'm unfamiliar with. These are the formats I'm familiar with:

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> (HTML 4)
<meta charset="UTF-8"> (HTML 5)

You can also set the content type in the response header via PHP  without needing a <meta> tag

<?php header('Content-Type: text/html; charset=utf-8'); ?>
  • Like 1
Link to comment
Share on other sites

  • 0

Although particularly unrelated, I'd recommend using utf8mb4.

 

https://mathiasbynens.be/notes/mysql-utf8mb4

 

but I highly doubt this will fix your issue.

 

 

No :( it does not :( someone suggested I have to convert it with html entities? opinions? 

 

You can try specifying the charset that PDO uses in your connection string to see if that helps:

<?php $link = new PDO("mysql:host=localhost;dbname=DB;charset=UTF8"); ?>

(From http://php.net/manual/en/pdo.construct.php#113498. If your PHP version is < 5.3.6, you'll need to look at: http://stackoverflow.com/a/4361485/4996362)

 

I'm also not sure if your <meta> tag is formatted correctly or if it's just a format I'm unfamiliar with. These are the formats I'm familiar with:

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> (HTML 4)
<meta charset="UTF-8"> (HTML 5)

You can also set the content type in the response header via PHP  without needing a <meta> tag

<?php header('Content-Type: text/html; charset=utf-8'); ?>

well I know this works in some instance as it fixed german characters, just didnt like turkish ones... :( 

Link to comment
Share on other sites

  • 0

It sounds like your data is in the wrong encoding, you don't need to use HTML entities if you're using UTF-8, and if only some text works when using UTF-8 then the source encoding is wrong.

Make sure MySQL is using the right encoding (utf8mb4 as mentioned before), and that the data in your database is in the right encoding. UTF-8 won't help you if you stick "Windows-1254" data directly into the database.

Link to comment
Share on other sites

  • 0

before your query you need to set it to UTF8

 

so try this after you connect

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");

using PDO something like this

$pdo = new PDO($dns, 'db_user', 'db_pass',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$pdo->exec("SET CHARACTER SET 'utf8'"); 
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.