• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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

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
csehz
Asked:
csehz
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
csehzIT consultantAuthor Commented:
That is amazing, tried putting it to the new PDO object and it works , thanks very much for your help
0
 
Ray PaseurCommented:
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
 
csehzIT consultantAuthor Commented:
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
 
Ray PaseurCommented:
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now