Solved

MySQL - 'SET NAMES latin2' for correct appearing of non english characters

Posted on 2015-01-16
5
221 Views
Last Modified: 2015-01-17
Dear Experts,

I have the below Php code which logins to my MySQL database on Yahoo Small business account, it sure works and ensures that the non-english characters in later queries appear correctly. (if the SET NAMES latin2 is not here, then there are ? marks at some characters)

<?Php
$connect = mysql_connect("mysql", "xx", "xx") or
			die ("Check your server connection.");
		$k = "SET NAMES latin2"; mysql_query($k);
		mysql_select_db("lang") or die(mysql_error());
?> 

Open in new window


But now testing different login method with the below PDO code but being not sure where and in which format could add this SET NAMES latin2?  

<?Php
$dbhost_name = "mysql";
$database = "xx";
$username = "xx"; 
$password = "xx"; 

try {
$dbo = new PDO('mysql:host=mysql;dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();

}
?>

Open in new window


Could you please advise in this question

Thanks in advance,
0
Comment
Question by:csehz
  • 2
  • 2
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
I found a couple of ways to do this.

in the config file:
database.params.driver_options.1002 = "SET NAMES latin2"

1002 is the value of constant PDO::MYSQL_ATTR_INIT_COMMAND (You can't use the constant in the config.ini)


or, when you create the PDF object:
$pdo = new PDO(
    'mysql:host=mysql.example.com;dbname=example_db',
    "username",
    "password",
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES latin2"));


hope this helps
0
 
LVL 1

Author Closing Comment

by:csehz
Comment Utility
That is amazing, tried putting it to the new PDO object and it works , thanks very much for your help
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I understand Latin2 to be ISO-8859-1, so if that's not what you're talking about this may be a longer conversation.
http://en.wikipedia.org/wiki/ISO/IEC_8859-2

Please have a look at this chart, then come back to the rest of the answer.
http://commons.wikimedia.org/wiki/File:UnicodeGrow2b.png

The problem with this character set is that it has single character code points above decimal 127 and below 255.  As a result, these characters conflict with the now-dominant standard UTF-8.  It's a plain fact that UTF-8 is taking over the world, and even PHP is getting on board.  At PHP 5.4, several of the functions that used to default to ISO-8859-1 character sets now assume UTF-8.

Some of the sites that use UTF-8: Google.com, Facebook.com, Youtube.com, Yahoo.com, Baidu.com, Wikipedia.org, Twitter.com, Amazon.com, Linkedin.com and the list goes on.  I'm sure by now you've come to the same conclusion that the rest of us have -- UTF-8 is the only way to go.  This article shows some of the backstory and explains how to make the conversion, which is pretty easy once you get the hang of it.  There are a collection of references at the end.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
 
LVL 1

Author Comment

by:csehz
Comment Utility
Sorry Ray your wrote exactly in that moment when I tried the before proposed solution.

So can not give points but will look into these examples, because yes it is very complicated I would like to display hungarian & polish characters on one page from a MySQL database and only with this putting SET NAMES latin2 worked so far, however would like to understand it deeper why. Thanks for the advices, probably will have some new questions yet on EE in the topic
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Not worried about the points, just wanted to share what I've researched and learned about character encoding.  Best of luck with it, ~Ray
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now