Forum Moderators: coopster

Message Too Old, No Replies

Howto Enable UTF-8 in PHP?

         

HoboTraveler

4:47 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi All,

I've enabled UT8 in the MySQL config, PHP config and Apache config.

I can view the UT8 characters correctly in phpMyAdmin. The data is stored correctly in the MySQL db.

However, when I call the data on a PHP page, PHP displays a? instead of the characters from the db.

I've enabled UTf8 in the php.ini
default_charset = "utf-8"

I've also called the header in the PHP page with the UTF8:
header('content-type: text/html; charset: utf-8');

Is there something that I've missed? What do I need to do to get my PHP pages to display the character set correctly?

Note: phpMyAdmin displays UTF8 correctly from the db.

Thanks

gosman

5:28 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi HoboTraveler.

I had the same trouble. Try this

<!-- xml version="1.0" encoding="UTF-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<?php echo $utf8variable;?>
<body>
<html>

HoboTraveler

6:41 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hello,

That did not work.

Anyone have suggestions?

TIA

PHP_Chimp

6:51 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried with more than 1 browser, to check if this is a browser issue, rather than a php/html issue?

HoboTraveler

7:11 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Yes, I tested the script on Internet Explorer, Firefox an Opera. The? appeared on all three browsers.

This seems like an issue with PHP. hmm.. I guess if that was the case phpMyAdmin would display the? too but phpMyAdmin has no issues.

TIA

[edited by: HoboTraveler at 7:13 pm (utc) on Jan. 21, 2008]

jatar_k

7:17 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you setting content-type properly in the html document?

ksks

9:49 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Try
<?php
mb_internal_encoding("UTF-8");
?>

first line of php code...I had to do this when trying a Bulgarian -> English translator type project.

mipapage

11:19 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In firefox, right click and view page info. Check for the "Encoding". What does it say?

HoboTraveler

5:19 am on Jan 22, 2008 (gmt 0)

10+ Year Member



@ksks,

The mb_internal_encoding did not work.

@mipapage,

Firefox > Character Encoding > is on Unicode(UTF-8)

Internet Explorer > is on Unicode(UTF-8)

mipapage

6:58 am on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay HoboTraveler,

You know that the page is in utf-8, so my guess is that you are double utf-8 encoding somewhere. Sounds strange, but I seem to recall having done this myself somewhere. Besides the two things you outlined in your first message, is there anything else you are trying in the process to get these characters to utf-8?

HoboTraveler

7:52 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Hello,

I've listed below the contents of my AMP config files. These are the only config areas where I've enabled UTF8.

php.ini contents
default_charset = "utf-8"

my.cnf contents
[client]
default-character-set=utf8

[mysqld]
character-set-server=utf8
default-character-set=utf8
default-collation=utf8_unicode_ci
character-set-client = utf8

httpd.conf contents
AddDefaultCharset UTF-8

and the php script itself has this in the first line:
header('content-type: text/html; charset: utf-8');

TIA

omoutop

9:26 am on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



since you have covered all aspects of headers and encoding...

perhpas your data were saved in db in utf-8 encoding.... have you tried to utf8_decode() your data to screen?

long shot here, but there is nothing else i can think of

HoboTraveler

10:30 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Hello,

The utf8 decode did not work.

I've listed two records from the table. These are greek characters.

&#913;&#928;&#927;&#923;&#937;&#925;&#906;&#913;k
&#931;&#906;&#934;&#925;&#927;&#931;

Maybe someone could insert these in a UTF8 MySQL and call them from a PHP script.

TIA

omoutop

10:49 am on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



first line read APOLONIA and second line reads SIFNOS (in greek characters), all capitalized

i am curious... did this problem appeared during the transfer of a database export to another db?

HoboTraveler

11:31 am on Jan 22, 2008 (gmt 0)

10+ Year Member



@omoutop,

You are correct. The first line is APOLONIA and second is SIFNOS (greek).

This issue did not occur between databases. I imported the characters correctly into MySQL and have not been able to read them from my PHP scripts since.

This forum encoded the Greek characters that I pasted earlier.

How did you insert the characters into your db? Did you insert the string that I pasted? In my db, the names are inserted in the non-encoded format. That is, APOLONIA and SIFNOS in greek.

Were you able to store the non-encoded names into the db and then read them correctly through your PHP script? If so, I would appreciate if you could share your PHP script.

Thank You

Romeo

11:32 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Besides the definitions already discussed, the connection between the client and server may use own connection-related character set and collation system variables.

So you may try to add a:
[mysqld]
init-connect='SET NAMES utf8'

to set this as a default for all connections, or start with these queries after your specific script connects to the database before sending other queries:
SET NAMES utf8;
SET CHARACTER_SET utf8;

Kind regards,
R.

omoutop

11:43 am on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



nope, i didnt insert them into db, i just pste them into a iso-8859-7 (greek) and a utf-8 encoding page to read them.
They read correclty in both pages.

HoboTraveler

12:01 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



@Romeo,

Adding the init-connect did not work.

I am pasting below the output for show variables. Maybe there is something there I may have missed.

mysql> show variables like 'c%';
+--------------------------+-------------------------------------------------------------------+
¦ Variable_name ¦ Value ¦
+--------------------------+-------------------------------------------------------------------+
¦ character_set_client ¦ utf8 ¦
¦ character_set_connection ¦ utf8 ¦
¦ character_set_database ¦ utf8 ¦
¦ character_set_filesystem ¦ binary ¦
¦ character_set_results ¦ utf8 ¦
¦ character_set_server ¦ utf8 ¦
¦ character_set_system ¦ utf8 ¦
¦ character_sets_dir ¦ /usr/local/mysql-standard-5.0.27-linux-i686/share/mysql/charsets/ ¦
¦ collation_connection ¦ utf8_general_ci ¦
¦ collation_database ¦ utf8_unicode_ci ¦
¦ collation_server ¦ utf8_unicode_ci ¦
¦ completion_type ¦ 0 ¦
¦ concurrent_insert ¦ 1 ¦
¦ connect_timeout ¦ 5 ¦
+--------------------------+-------------------------------------------------------------------+

TIA

HoboTraveler

12:38 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



It worked!

I was able to read the UTF8 characters correctly from the db!

What happened was, I was calling an includes file that contains the database connect info. The includes file connected to multiple databases, some latin and some UTF8.

I removed the includes file and tested by just calling the UTF8 database. The data displayed correctly.

How do I get UTF8 to work with the includes file? Is there a way to specify the UTF8 in the mysql_connect or mysql_select_db function? I guess that would selectively force UTF8 on the database that uses it?

TIA

omoutop

12:41 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try somethig like:
mysql_query(SET NAMES utf8);
after mysql connection.

HoboTraveler

12:43 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



Update:

I think I got it to work with multiple databases!

I've inserted the line:

mysql_query('SET NAMES utf8');

just after the mysql_connect() and it seems to work. I though this line was not needed since UTF8 is forced in the MySQL config file..?

Thank You all for your help.

omoutop

12:50 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nice!
good job and well done :)

mipapage

1:23 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've listed two records from the table. These are greek characters.

&#913;&#928;&#927;&#923;&#937;&#925;&#906;&#913;k
&#931;&#906;&#934;&#925;&#927;&#931;

Is that what you see in phpmyadmin in the textbox/area for that data? If so, Ive seen this before but cannot recall exactly what we did to sort it out.

We had a mysql db storing Cyrillic characters as entities, as you do, but you want mysql to store the utf-8 characters. What we did here was to work over the DB so that it stored the actual utf-8 characters, not the entities.

I'll have to poke around but I remember using some functions that I found on php.net, including utf8dec, uft8html2utf8 and some others.

mipapage

1:24 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, looks like I was too late. Glad you got it sorted out :)
 


 


 


 

Status: 403 Forbidden