Php/MySQL with Encoding Problems

Character encoding issue with PHP/MySQL

(Since this clearly shows a PDO omission, I am reopening)

One-line change:

$pdo = new 
PDO("mysql:host=$servername;dbname=$dbname;dbname=db;charset=utf8mb4'",
$username, $password);

References:

http://mysql.rjweb.org/doc.php/charcoll#php

Trouble with UTF-8 characters; what I see is not what I stored (Look especially for "black diamond".)

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.

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 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 encoding problems. � instead of certain characters

As mentioned by others, you need to convert to UTF8 from end to end if you want to support "special" characters. This means your web page, PHP, mysql connection and mysql table. The web page is fairly simple, just use the meta tag for UTF8. Ideally your headers would say UTF8 also.

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

Set your PHP to use UTF8. Things would probably work anyway, but it's a good measure to do this:

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');

For mysql, you want to convert your table to UTF8, no need to export/import.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8

You can, and should, configure mysql to default utf8. But you can also run the query:

 SET NAMES UTF8

as the first query after establishing a connection and that will "convert" your database connection to UTF8.

That should solve all your character display problems.

PHP MYSQL encoding issue ( Turkish Characters )

  1. Double check your HTML encoding.
  2. http://php.net/manual/en/mysqli.set-charset.php (just after you connect to DB: mysqli_set_charset($con, 'utf8');
  3. Are you using prepared statements? (not related but important).


Related Topics



Leave a reply



Submit