Please help me with these strings

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
devdept
Forum Newbie
Posts: 4
Joined: Mon Apr 07, 2008 5:22 pm

Please help me with these strings

Post by devdept »

Hi All,


I am getting crazy behind these two damn string :banghead: :

Code: Select all

TEZ YAPI ?N?AAT TAAH. MÜH. B?LG. T?C. VE
+44 (0588) 555 6666
The first is a real turkish company name, the second a trivial phone number.

I can't, again I can't, find a reliable way to store them in a mySQL database and restore them correctly because of the international chars and the plus sign.

I would love to see a very easy (working :crazy: ) example using PHP / Sessions / mySQL composed by form.php, process.php and display.php files and a test mySQL table.


Very grateful to you all,

Alberto
samb0057
Forum Commoner
Posts: 27
Joined: Wed Mar 26, 2008 9:51 am

Re: Please help me with these strings

Post by samb0057 »

When you create your MySQL tables, did you use the "utf8_general_ci" collation?
devdept
Forum Newbie
Posts: 4
Joined: Mon Apr 07, 2008 5:22 pm

Re: Please help me with these strings

Post by devdept »

No, how should I do?

Thanks,

Alberto
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Please help me with these strings

Post by onion2k »

samb0057 wrote:When you create your MySQL tables, did you use the "utf8_general_ci" collation?
The collation makes no difference to the way the data is stored, just the way it's sorted when you fetch results. The important thing is the table charset, and getting the MySQL client, PHP and the HTML all to use the same encoding.

When you create your database table you need to include the CHARSET directive.. eg

Code: Select all

CREATE TABLE IF NOT EXISTS `advert` (
  `advert_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(255) collate utf8_unicode_ci NOT NULL DEFAULT '',
  `publish_date` date NOT NULL DEFAULT '0000-00-00',
  `publish` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY  (`advert_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;
 
That will make MySQL store the data as UTF8 if you send it UTF8 data rather than converting it to something else.

You'll also need to put PHP's MySQL client into UTF8 mode using..

Code: Select all

mysql_query("SET NAMES utf8");
It's best to do that immediately you connect to the database.

And finally you need to make sure the page is displayed as UTF8 too... Personally I do that with a header().

Code: Select all

header("Content-Type: text/html; charset=utf-8");
There are probably easier ways to do it, but that's what works for me.
devdept
Forum Newbie
Posts: 4
Joined: Mon Apr 07, 2008 5:22 pm

Re: Please help me with these strings

Post by devdept »

So any app dealing with international chars does what you explained to aviod troubles?

Where can I find more info about UTF8, what exactly does?

Thanks so much again,

Alberto
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Please help me with these strings

Post by onion2k »

devdept wrote:So any app dealing with international chars does what you explained to aviod troubles?
Honestly, I have no idea. That's the solution I developed for my sites. Other sites might do the same, they might not. I've never bothered looking.
devdept wrote:Where can I find more info about UTF8, what exactly does?
Google is a good place to start.
devdept
Forum Newbie
Posts: 4
Joined: Mon Apr 07, 2008 5:22 pm

Re: Please help me with these strings

Post by devdept »

I found the following page.

http://en.wikipedia.org/wiki/Utf8


Thanks a lot again.

Alberto
Post Reply