Question

EDIT/UPDATE TO CODE:
this fixed my problem.

$con = new PDO("mysql:host=$hostname;dbname=XXXXX;charset=utf8", $dbusername, $dbpassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); 

I am trying to enter some Cyrillic text into my database. The PHP script connects ok and adds the data to the database. The problem is, that when ever the form submits Cyrillic text in enters the Cyrillic characters it in the database like this "рты Ð´Ð»Ñ Ð¿Ð»Ð°Ð²Ð°Ð½Ð¸Ñ"

Any help would be great, thanks.

The database collation is set to "utf8_general_ci". The php file is saved as "uft8"(hostgator host-if that helps).

I have this at the top of the page.

<?php header("Content-Type: text/html; charset=utf-8"); ?>

I have added this to the header of my file:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

The connection to the database is like this:

$con = new PDO("mysql:host=$hostname;dbname=XXXXX;charset=utf8", $dbusername, $dbpassword); 

Example of the form:

<form name="form" id="form" action="/form.php" method="post" accept-charset="utf-8">
<input type="hidden" name="foo" id="foo1" value="<?php echo $foo1; ?>"><?php echo $foo1; ?>   
<input type="hidden" name="bar" id="bar1" value="<?php echo $bar1; ?>"><?php echo $bar1; ?>  
<input type="submit" id="submit" value="submit"/>

**the php echos the Russian text properly on the page.

Example of my PHP SQL insert:

 $sql = "INSERT INTO foobar (foo1,bar1) VALUES (:foo1,:bar1)";
   $q = $con->prepare($sql);
   $q->execute(array(':foo1'=>$foo1, 
             ':bar1'=>$bar1));
Was it helpful?

Solution

Please try to

$con->exec("SET NAMES = utf8");

after PDO object creation.

It is normal, that your database stores this content this way. Try to check your MySQL client, can it display UTF-8 database data properly.

I know that some editions of SQLYog couldn't, such as older editions of PHPMyAdmin

I bet that you will receive proper data when you will take it from the script. If not - try to use "SET NAMES" as I already said.

It may help you.

Thanks.

Also, take a look at PHP PDO: charset, set names?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top