views:

23

answers:

2

Hi guys, I have a mysql database tabel populated with non english data. When I view the data in Navicat MySQL browser the data appears fine. However when I run a php script to seelct and display the data on a web page it displays question marks instead. The page encoding is set to utf8 and even the mysql collation is set to utf8 - something gets wrong in selecting and displaying... please help.

+2  A: 

MySQL connection settings could be at fault here. Run this MySQL command when you connect to the database from PHP, before you run any other SQL commands:

SET names 'utf8';

This should set the connection's encoding to UTF-8. As you're saying, the page and the database is already in UTF-8 (that should also mean the page sends Content-Type: text/html; charset=utf-8); the connection itself can accidentaly have a different encoding by default :(

Piskvor
THANK YOU THANK YOU :D THAT WAS EXACTLY IT - WORKS like a charm!
Ali
Ali, then you should select this answer..
sims
@Ali: You're welcome. It's a less known feature of MySQL.
Piskvor
+1  A: 

In addition, in HTML, inside we must write:

<meta charset="utf-8">

When we create a MySQL database, we must use something like this:

CREATE DATABASE `base` DEFAULT CHARACTER SET=utf8;

When we create tables, use:

CREATE TABLE `base`.`table` (
`key` int(5) unsigned NOT NULL,
`name` varchar(100),
...
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

In PHP code, after mysql_connect() and mysql_select_db(), use:

mysql_query('SET NAMES UTF8');
gespadas
In the first part, I assume you mean "the document must have a charset in addition to Content-Type"; this can be sent either in the headers (as `Content-Type: text/html; charset=utf-8`), or in the HTML HEAD (as `<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />`). Your syntax is HTML 5, which isn't yet universally supported. Also, when creating a table, it's a good idea to set the collation together with the charset (charset determines supported characters, collation determines the sorting order within that charset - e.g. "...rŕřsštť...") Otherwise, good post.
Piskvor