Incorrect encoding from PHP?

2019-09-12 10:31发布

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...

5条回答
戒情不戒烟
2楼-- · 2019-09-12 11:01

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

查看更多
我只想做你的唯一
3楼-- · 2019-09-12 11:01

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

查看更多
干净又极端
4楼-- · 2019-09-12 11:03

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');
查看更多
贼婆χ
5楼-- · 2019-09-12 11:03

In your HTML code, try:

<meta http-equiv="content-type" content="text/html; charset=utf-8">
查看更多
小情绪 Triste *
6楼-- · 2019-09-12 11:13

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.

查看更多
登录 后发表回答