Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uft8 : correct in MySQL - fails in PHP

Status
Not open for further replies.

dkdude

Programmer
Jun 16, 2003
849
DK
I can't get PHP to read/output utf8 text from MySQL.

[tt]Setup - win32, MySQL 5.1.30, PHP 5.2.8[/tt]

My table is created like this:
Code:
CREATE TABLE `pages` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `path` varchar(100) DEFAULT NULL,
  `pageTitle` varchar(100) DEFAULT NULL,
  `updatedOn` datetime DEFAULT NULL,
  `html` text,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

A sample row-dump from the MySQL prompt, with special characters (Danish, in this case) looks correct, like this:
Code:
       ID: 6
     path: /test/
pageTitle: uft8-test
updatedOn: NULL
     html: ÆØÅæøå

So far, so good. However, when I fetch the [tt]html[/tt] field from this row and output it with PHP I get only;

Code:
??????

I output from PHP with utf8 encoding set in my xhtml head block:

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

Stars, free drinks and eternal gratitude in exchange for advice :)

Thanks
 
sounds like a mysql question but try executing this query just after you create the connection

Code:
mysql_query("set names utf-8");

 
[0]
>A sample row-dump from the MySQL prompt, with special characters (Danish, in this case) looks correct
This seems to betray the problem. If you dump the data in the mysql prompt hosted in the Danish command prompt windows and it displays correctly, it would suggest the text is inserted into the db as something like windows-1252 encoding. Instead, if it is inserted in proper utf-8 encoding, the dump would show up something akin to garbage.

[1] With that reasoning, I suggest you try this first to ascertain the real cause of the problem. Suppose the text column is fetched and is stored in the variable $html. Instead of this.
[tt] echo $html;[/tt]
replace it by this.
[tt] echo iconv('windows-1252','utf-8',$html);[/tt]
 
Also, it is a bit weird to have to crack the safe to find it's key inside it. So embedding the encoding inside the page just does not make any sense. Send a header instead, so the browser does not have to reparse the page.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top