Incorrectly rendered utf8 characters as question marks after conversion to mysqli_query ()

My database is latin1_swedish_ci, but all tables containing foreign characters (German, Turkish ...) are utf8_general_ci.

Before upgrading to php 5.6 I used

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

      

before mysql_query()

and everything was displayed correctly on my page ( <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

in the page header).

After converting all mysql_query(...)

to mysqli_query(id,...)

and running under php 5.6, are all foreign languages ​​now scrambled? and. Moving to php 5.4 doesn't help. phpMyAdmin displays the mysql database correctly (which hasn't changed).

I was looking for a solution but nothing seems to work ... Am I missing something?

What do I need to change in my code to work properly?

+3


source to share


2 answers


After searching over and over ...

MAMP MySQL does not recognize my.cnf values ​​on OSX

Is MySQL included in MAMP without a config file?

http://www.toptal.com/php/a-utf-8-primer-for-php-and-mysql

Here is the solution.

In my php scripts, I had to add a charset request after connecting to the database.

$con=mysqli_connect($host",$user,$password,$db);
mysqli_set_charset($con,"utf8");

      



The previous encoding and names I used with mysql_query () prior to php 5.4 were insufficient.

mysqli_query($con,"SET CHARACTER SET utf8;");
mysqli_query($con,"SET NAMES utf8");

      

On my local server, I also had to recompile mysql after adding my.cnf file containing the following lines:

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
default-storage-engine = InnoDB
character-set-client-handshake
collation-server=utf8_general_ci
character-set-server=utf8
[mysqld_safe]
default-character-set=utf8

      

I also had to add utf8 encoding to MAMP by editing MAMP / Library / share / charsets / index.xml and adding the following lines:

<charset name="utf8">
  <family>Unicode</family>
  <description>UTF8 Unicode</description>
  <alias>utf8</alias>
  <collation name="utf8_general_ci" id="33">
   <flag>primary</flag>
   <flag>compiled</flag>
  </collation>
  <collation name="utf8_bin"        id="83">
    <flag>binary</flag>
    <flag>compiled</flag>
  </collation>
</charset>

      

On my web server, the 2 steps above were not necessary.

+3


source


These timesheet characters are the result of a browser or other user-visible software package displaying utf-8 characters as if they were ASCII or Latin-1.

REV. ... In Chrome browser, you can view the encoding with which the browser is viewing your page. Click in the chrome menu (three small horizontal lines) in the upper right corner. Click More Tools> Encoding> Then you will see a selection of character sets. Try to choose another one.

Try to put this line in your HTML documents <HEAD>

.

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

      

This can force the browser to use the correct character set.

END edit



After your switch to mysqli, did you keep the queries SET CHARACTER SET

and SET NAMES

when you open a mysql session? If not, put them back and see if that helps.

Your database may be working correctly, but php is telling the browser that you are using Latin-1. This is actually very likely because phpmyadmin is working correctly.

Try this: Setting PHP default encoding to utf-8?

There's a lot of conceptual confusion around character sets and collations. When you say

My database is latin1_swedish_ci

you mean the default character set for the newly defined table latin1

and the standard collation is case insensitive. These are just the defaults. As far as data storage is concerned, this is the character set used for each column. You say that utf8

. Sort ( utf8_general_ci

) is only used for searching and matching.

+1


source







All Articles