views:

28

answers:

2

Hello,

I'm fetching data from external database (I cannot edit it so don't suggest that please) which has internal encoding set to cp1250_general_ci.

I need to display that data as UTF-8 but I cannot get it to work. I'm using this to fetch the data:

  $dsn = 'mysql:dbname=eklient;host=127.0.0.1';
  $user = 'root';
  $password = 'root';

  try {
      $dbh = new PDO($dsn, $user, $password);
  } catch (PDOException $e) {
      $this->view->warning = 'Connection failed: ' . $e->getMessage();
  }

  $sql = 'SELECT * FROM zam z WHERE z.id_object = :id_object;';
  $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  $sth->execute(array(':id_object' => 1));
  $data = $sth->fetchObject();

The original string is:

poznamka<br /> riadok1 ľščťžýáí

When I print it like this:

echo iconv('Windows-1250', 'UTF-8', $data->poznamka);

I get this:

poznamka<br /> riadok1 ?š??žýáí

So some characters are getting substituted with question marks. Any idea how to solve this?

Yes I do have correct meta tags in HTML.

+1  A: 

Have you looked at this comment from the PHP MySQL PDO driver infosite:

This is the way to force mysql PDO driver to use UTF-8 for the connection :

<?php
$pdo = new PDO(
    'mysql:host=hostname;dbname=defaultDbName',
    'username',
    'password',
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
?>
DrColossos
+1  A: 

Meta tags in HTML don't really make a difference; you need to have the correct header sent, you can do this by doing

<?php
header('Content-Type: text/html; charset=utf-8');
nathan
headers were ok but thanks anyways... +1
Richard Knop
cool, glad you got it sorted :)
nathan