Converting PHP to unicode (UTF-8) format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Converting PHP to unicode (UTF-8) format

Post by Leao »

Hi, I'm using the following PHP code to retrieve and print text from a mysql database. I have created a Flash movie that retrieves the text generated by this PHP file and displays it. For some reason some of the characters visible in the PHP file are invisible in the Flash movie, such as the characters '+' and '€'. I have spoken to a Flash expert about this and he suggested encoding my PHP file as unicode (UTF-8) format as Flash prefers this. Do you know how I can do this?

Thank you,

Leao

Code: Select all

<?php
mysql_connect("mysql.myurl.com","username","password") or die(mysql_error());
mysql_select_db("username_database") or die(mysql_error()); 

$result = mysql_query("SELECT * FROM database")
or die(mysql_error());  

$row = mysql_fetch_array( $result );

echo $row['landline'];
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

This really has nothing to do with the encoding of your PHP file. We need to know a few things:

1. What version of MySQL are you using?
2. If you are using 4.1 , what encoding is your table set at?

At least you'll need to convert the data in the database to UTF-8 on loading (probably from ISO 8895-1), but ideally the data gets stored in the database as UTF-8 to begin with.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

You may alter the table character-set to utf8 if you are using MySQL 4.1 or higher. Also, you may need to execute the following query to let your MySQL server know what character-set you want to use.

Try

Code: Select all

<?php
header('Content-Type: text/plain; charset=utf-8');
$connection = mysql_connect("mysql.myurl.com","username","password") or die(mysql_error());
mysql_query('SET NAMES utf8', $connection);
mysql_select_db("username_database", $connection) or die(mysql_error());

$result = mysql_query("SELECT * FROM database")
or die(mysql_error()); 

$row = mysql_fetch_array( $result );

echo $row['landline'];
?>
Hope this will solve your problem.

Cheers,
Dibyendra
Leao
Forum Commoner
Posts: 49
Joined: Mon Aug 21, 2006 8:57 pm
Location: London

Post by Leao »

Thanks. I'm using MySQL version 4.0.27. How do I find out in PHP myAdmin what encoding my table is set to?

Thanks again,

leo
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

for 4.0.x encoding is set on a per-server basis
Post Reply