PHP MySQL Charset Utf8 Problems

PHP mysql charset utf8 problems

Try this

<?php

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

and then in the connection

<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>

PHP & MySQL - Problems with UTF-8 encode

Keep in mind that collation is not the same as charset. You need to set your database and table to be in UTF-8 encoding. This can be done with running this SQL command (only need to be done once).

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Furthermore, you should set the mysql_* connection to UFT-8. This code should be placed directly after connecting to your database.

mysql_set_charset("utf8");

I see you already set the PHP header to UTF-8 as well, so that's good.

Keep in mind that usage of mysql_* functions are deprecated and no longer maintained; you should switch to PDO or MySQLi for security reasons.

If neither of these steps helped you, you may need to save the document itself as UTF-8 w/o BOM, this can be done in Notepad++ as Format -> Convert to UFT-8 w/o BOM.

UTF-8 problems PHP/MySQL

Make sure the connection to your database is also using this character set:

$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);

According to the documentation of mysql_set_charset at php.net:

Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.

See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php

Check the character set of your current connection with:

echo mysql_client_encoding($conn);

See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php

If you have done these things and add weird characters to your table, you will see it is displayed correct.

UTF 8 encoding not working properly in PHP

The character set needs to be defined in a few different places:

The MySQL database

The text stored in the database might not be encoded as UTF-8. You can define a default character set as part of the create database statement:

CREATE DATABASE mydb CHARACTER SET utf8;

You can also specify per-column character sets with create table.

Within your PHP code

You'll need to tell your client-side code which encoding it should use when communicating with the database.

If you're using PDO, you can specify the character set as part of the DSN string:

$dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';
$dbh = new PDO($dsn, $username, $password);

If you're using MySQLi, you can use the mysqli_set_charset() function/method:

$dbh->set_charset('utf8');

or:

mysqli_set_charset($dbh, 'utf8');

Alternatively, the MySQL website suggests issuing a statement after connecting to the server:

SET NAMES 'utf8';

Within the HTML output

For HTML5, you can simply add the following <meta> tag within the <head> element of your output:

<meta charset="utf-8">

PHP MySQL utf 8 encoding

Set the connection to use UTF-8:

<?php

// MySQLi:

$connection = new MySQLi( /* ... credentials ...*/);
$connection->set_charset("utf8");

// MySQL:
$connection = mysql_connect(/* ... credentials ... */);
mysql_set_charset("utf8", $connection);

?>

PHP output encoding issues with UTF-8 strings from MySQL databases

You should not change all the character_set% fields, just the three that are affected by SET NAMES utf8;.

Don't use utf8_encode or decode.

You have probably messed up when storing.

This seems to recover the characters, but this not a viable fix:

CONVERT(CAST(CONVERT('pürson from “Vancouver, Canadaâ€' USING latin1)
AS BINARY)
USING utf8)
--> 'pürson from “Vancouver, Canada - spec',

In order to figure out what was done, please provide

SELECT col, HEX(col) FROM tbl WHERE ...

for some cell that is not rendering properly.

Php + Mysql (UTF-8 ) some characters are still bug

Checklist for Problems with character/charset/collation

Including mysql, mysqli, PDO



Content

  1. DISCLAIMER
  2. My insert's in my DB doesn't work properly! What can i do?
  3. Change Charset and Collation of a Database or Table
  4. Set the encoding of your skript files
  5. Set the charset of your page with php or meta tag
  6. What's the difference between UTF8 and UTF8mb4?
  7. Answer to this specific Question
  8. Further Information/Additional Links
  9. Side Notes


1. DISCLAIMER

This Answer should not only answer this question, also should the answer be a bit more extensive, so more people find faster a bundled and good answer!

!Important Notice!

If you change something in your Database always make sur you have a backup of your database! Check it 2 times, or 3!

I'm open for improvements and comments, such as error corrections.

In addition I apologize if the grammar is not perfect: D


If you get stuck on a question like this:

  • Php + Mysql (UTF-8, utf8mb4) some characters are still bug
  • How to convert an entire MySQL database characterset and collation to UTF-8?
  • “Incorrect string value” when trying to insert UTF-8 into MySQL
  • Change MySQL default character set to UTF-8 in my.cnf?
  • Using utf8mb4 with php and mysql
  • PDO + MySQL and broken UTF-8 encoding
  • Error in insertion data in php Mysql
  • PHP PDO: charset, set names?
  • SET NAMES utf8 in MySQL?
  • PHP mysql charset utf8 problems
  • UTF-8 all the way through
  • Manipulating utf8mb4 data from MySQL with PHP
  • ERROR 1115 (42000) : Unknown character set: 'utf8mb4' in mysql

...then my answer maybe helps you!


2. My insert's in my DB doesn't work properly! What can i do?

If your insert's doesn't work properly an your inserted data looks something like this in your database then this could have various reasons!

Examples:

??????????
br>�??_ �?�
â_ ⬠⥠J

Here is a little checklist you can go trought and check if everything is how it should be!

(After the checklist there a few extra informations for mysql, mysqli and PDO)


Checklist:

  • Make sure default character sets is set on tables, client, server & text fields

    • If NOT See Point 3
  • Make sure your database connections character sets

    • IF NOT See Point mysql/PDO
  • Make sure if your displaying data that the charset of the document is set!

    • IF NOT See Point 5
  • Make sure your skript files are saved with the right charset!

    • IF NOT See Point 4
  • Make sure you set your character and your charset!

    • IF NOT See Point mysql/PDO
  • Make sure you forms accept utf8!

    • IF NOT See Point 5
  • Make sure you have set the connection encoding

    • IF NOT See Point mysql/pdo
  • Make sure you have set the servercharacter encoding right

    • IF NOT See Point mysql/pdo
  • ...

  • You have to be sure your using utf8/ utf8mb4 everywhere!


mysql:

-mysql_query("SET NAMES 'utf8'"); Run SET NAMES before every query you use. Because if a mysql driver don't provied mechanismus to charset then you have to use SET NAMES!

-mysql_query("SET CHARACTER SET utf8 "); Set character to utf8

-mysql_set_charset('utf8'); Set your charset to utf8

-mysql API driver doesn't support utf8mb4 (ERROR 1115 (42000))

-character_set_server=utf8 to set server character

PDO:

-$dbh->exec("set names utf8"); If your using PDO you can use this line to SET NAMES

-$dbh = new PDO("mysql:host=$host;dbname=$db;charset=utf8"); This line set the charset but you have to have PHP 5.3.6 or higher

-$dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' "); You can also set SET NAMES with this line

-mb_internal_encoding('UTF-8'); to set the encoding when you use PDO


3. Change Charset and Collation of a Database or Table

If you have to change the charset or collation of a database or table you can use these lines of code:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;


4. Set the encoding of your skript files

You may have to check that your skript(php) files are saved with the right charset!

For this i would recommend you Notpad++!

If you have opened your file in notpad go to the menupoint 'Encoding' and change the charset


5. Set the charset of your page with php or meta tag

For displaying data in utf8/utf8mb4 you have to be sure you site is set with the right charset!

You can set the charset in 3 ways like this:

//PHP
ini_set("default_charset", "UTF-8");
header('Content-type: text/html; charset=UTF-8');

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

Also to accept utf8 in your form use:

<form accept-charset="UTF-8">


6. What's the difference between UTF8 and UTF8mb4?

UTF8:

-utf8 does only support symbols with 3 bytes

-...(many more)

UTF8MB4:

-utf8mb3 does support symbols with 4 bytes

-...(many more)


7. Answer to this specific Question

I think this should work since your using PDO:

(After you created a PDO object! If your using a PHP version less then 5.3.6)

$dbh->exec("set names utf8");

Otherwise try one of these:

ini_set("default_charset", "UTF-8");
header('Content-type: text/html; charset=UTF-8');

UPDATE:

To change the collation or charset of a database or table use this:

ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;


8. Further Information/Additional Links

  • default character set
  • character set
  • mysql_set_charset
  • error_reporting
  • pdo
  • mysql
  • mysqli


9. Side Notes

9.1 Error Reporting

If Error's not get displayed use this code snippet:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
?>

9.2 Unicode

So that you don't make any mistake you have to really understand utf8!

9.3 One word to mysql, mysqli and PDO

My Personal ranking is:

  1. PDO
  2. mysqli
  3. mysql

I would recommend you to use PDO or mysqli, because the have many benefits against mysql!

Insert Update to MYSQL with PHP 5.5 UTF-8 issues

try

AddDefaultCharset utf-8

in you apache configuration

or in you php file use

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

and also try to

default_charset = "utf-8";

in you php.ini

also you can try with

  [client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
character-set-client-handshake = false #force encoding to uft8
character-set-server=utf8
collation-server=utf8_general_ci

[mysqld_safe]
default-character-set=utf8

in your my.ini and restart mysql demon

Problems with charset in PHP/MySQL

try to change your column where you store this date to utf8 also

here some codes you may use.

Change the character-set/collation (database):

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8;

Change the character-set/collation (table):

ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8;

Change the character-set/collation (columns):

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;



Related Topics



Leave a reply



Submit