Utf-8 Problems PHP/Mysql

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.

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 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);

?>

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 not displaying some UTF-8 characters correctly

Consider the following things in this situations.

Make sure the header match the following

Content-Type:text/html; charset=UTF-8

and try setting UTF-8 in html

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

The source file encoding can make problems when string are hard coded in file. Since this file echoing it that seem to be no issue.



Related Topics



Leave a reply



Submit