0

1) I have a table tbl_Data in database which has name column with text comparision method (?) property set toutf8_polish_ci. Works as a charm, when I'm browsing tbl_Data through phpMyAdmin.

2) In my html code I've got:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">

so seems like I'm sending proper encoding headers for browser...

3) My PDO dsn contains ;charset=UTF-8, followed from php manual.

4) In my php code I use:

foreach(parent::query('SELECT ID,PLName,LatinName from tbl_Data') as $row) {
    $result = $result."
        <tr>
            <td>".utf8_encode($row['PLName'])."</td>
        </tr>
    ";
}

Having all of this I'm still getting 'garbage' (=?) chars instead of proper polish letters, though some of them are displaying well (phpMyAdmin shows all properly). What I am missing here? Please advice, fellows!

My MySQL Engine is InnoDB, webserver: nginx with fpm if it is revelant...

Darj
  • 1,403
  • 1
  • 17
  • 47
  • 1
    Did you see the comment on the php manual for [utf8_encode function](http://php.net/manual/en/function.utf8-encode.php)? namely: "If your text is already in UTF-8, you do not need this function"? What happens if you don't use the `utf8_encode` function? – Charlie Dec 28 '12 at 03:09
  • If I don't use this function at all, then I'm receiving � from "ó" which was correct earlier using `utf8_encode()`. Rest of missing letters are still "?" – Darj Dec 28 '12 at 03:23
  • maby this will help http://php.net/manual/en/function.htmlspecialchars.php – Ivo Dec 28 '12 at 03:33
  • What PHP version are you using? – CodeZombie Dec 28 '12 at 04:13
  • PHP 5.4.10 at this moment – Darj Dec 28 '12 at 17:24

5 Answers5

2

so seems like I'm sending proper encoding headers for browser.

Wrong. <meta> is not an HTTP header but HTML tag, a non-standard one. So, your code still lacks proper header.

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

My PDO dsn contains ;charset=UTF-8, followed from php manual.

if your PHP version is less than 5.3.6 it won't work either. use SET NAMES utf8 regular query instead

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I've added `header(...)` line just after my `ob_start();` line and the chars are still incorrect. The only difference is if I use `utf8_encode()` or not. If code uses it, only one letter is displayed correctly `ó`, if not none of them are. `php --version` gives me: `PHP 5.4.10 (cli) (built: Dec 22 2012 22:12:36) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies` – Darj Dec 28 '12 at 13:19
1

There is no reason to use utf8_encode() if your text is already UTF-8 encoded.

A potential issue might be your version of PHP. The ;charset=UTF-8 element of your DSN is only supported by PHP version >= 5.3.6. Prior to 5.3.6 this element was silently ignored (instead of issuing a warning). More information and a workaround for this are available in the docs: http://php.net/manual/en/ref.pdo-mysql.connection.php

CodeZombie
  • 5,367
  • 3
  • 30
  • 37
  • `php --version` in cli gives me: `PHP 5.4.10 (cli) (built: Dec 22 2012 22:12:36) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies ` so it seems like it should work... – Darj Dec 28 '12 at 13:22
1

Charset UTF-8 is good (in your html) :P

Try this.. (use new, experimental table for this, to check it out because I have no idea if your PHP script is cleaning $_POST entries for example, or doing something at all..)

  1. create column set to utf8_bin (not utf_ci_polish) named "polish_title"

  2. Before PHP passes (text input, polish) data to mysql INSERT (or update) covert those with this (PHP integrated) function

    $polish_input = htmlspecialchars($input_polish_data);

    INSERT INTO table_name (polish_title) values ('$polish_input');

You will see strange data in mysql table (via phpmyadmin) when polish signs are part of your input what is normal (or blobed), but good ones (polish native) on your site when you get mysql data out ;)

If this gonna work, ALL your mqsql (text) columns should be changed to utf8_bin. And to easy entries on yourself, you can create function which can "clean" all $_POST inputs on your website before those inputs engage to mysql DB.

Big plus on this is that visitors from other countries will see your native letters & signs (what is also important).

Edit (thx to Common Sense user input)

ANd yeah include this as a MUST also in your header

header('Content-Type: text/html; charset=utf-8');
Xfile
  • 674
  • 8
  • 19
0

I could not comment on your post.....so I suggest here~_~
From here, it said utf8_encode — Encodes an ISO-8859-1 string to UTF-8
therefore, my question is: is your output is ISO-8859-1?
may be you could create a normal English test record and try again.

Michael Law
  • 181
  • 1
  • 1
  • 12
  • Thanks for advice, I've added a test column with default settings and populated it with all possible polish letters. phpMyAdmin shows great, but website shows: `?????�??` – Darj Dec 28 '12 at 03:30
  • how about normal English, just like abcdefg? – Michael Law Dec 28 '12 at 03:33
  • I just search on stackoverflow, and guess you may refer to this one http://stackoverflow.com/questions/9241709/utf8-encode-does-not-produce-right-result – Michael Law Dec 28 '12 at 03:37
-1

In your HTML code, try:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
Levin
  • 194
  • 5