Issue with utf-8 encoding using PHP + MySQL

2019-01-25 04:27发布

问题:

I moved data from MySQL 4 (they were originally set to latin2 encoding) to MySQL 5 and set encoding to utf-8. It looks good in phpMyAdmin, and utf-8 is okay. However there are question marks instead of some characters on website! The website encoding is also set to utf8 so I dont understand where the problem is.

PHP and HTML files are also set to utf8.

I have no idea...

回答1:

try query

SET NAMES utf8

before any query in your application



回答2:

On my server, adding these to my php file had no effect:

ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');

But everything worked perfectly once I added this to the top of my php file:

$mysqli->query("SET NAMES 'utf8'");

Note: I am using encoding utf8_general_ci in my database, but utf8_unicode_ci works the same for me.

Hope that helps.



回答3:

Try setting the MySQL connection to UTF-8:

SET NAMES 'utf8'

And send explicit UTF-8 headers, just in case your server has some other default settings:

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


回答4:

You don't have to set your PHP and HTML files to utf-8.

You just have to set your output encoding to UTF-8 and the browser will display appropriately.

In HTML:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

In PHP:

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

When you get a string that is UTF-8 from the MySQL table, it will be UTF-8 all the way to browser output unless you convert the encoding. It's the way that the browser inteprets it.



回答5:

Put .htaccess file in your web-site root with content: AddDefaultCharset UTF-8

and

in your dbconfig set after connection to db:

mysql_query("SET NAMES 'utf8'");



回答6:

Here is a fix. Set the header to header ('Content-type: text/html; charset=utf-8'); Then print your content using utf8_decode($content). You must have the two to make it work.



回答7:

mysql_query("SET NAMES UTF8");

adding this line at the end of my "connection.php" solved my problem.

My connection file's complete code is:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_test = "localhost";
$database_test = "test";
$username_test = "username";
$password_test = "password";
$test = mysql_pconnect($hostname_test, $username_test, $password_test) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_query("SET NAMES UTF8");
?>

My database collation is "utf8_general_ci".

Pages are "dreamweaver default utf8" and "unicode normalisation form=C (Canonical Decomposition)".



回答8:

When you show UTF8 characters on a website but tell the browser to interpret them as Latin1 (or Latin2) you see this kind of gibberish: ß

When you show Latin1 (or Latin2) characters on a website, but tell the browser to interpret them as UTF8, you see question marks.

So my guess is that you switched everything to UTF8 (I mean, you told the DB Engine, the web server and the browser you would be using UTF8), but you didn't actually convert the strings to UTF8.

Do what @Darkerstar said. Convert your dump to UTF8 (Notepad++ can do that easily) and import it again.



回答9:

It doesn't seem like setting every SQL Database, Table, and Field to UTF-8 in MySQL and is good enough. Very annoying.

Ended up forcing the issue to solve encoding problems:

Had to use this Every place I open the database: $db->set_charset("utf8");

And that worked. Finally.



回答10:

I had this problem recently (I hope its the same problem you are having), I tried many ways but at the end what worked was really simple.

Convert your dumped SQL file to UTF-8 format and then import it.

BW: I used Notepad++ for the conversion.