3

Ok i inherited a pretty bad DB Situation the Views and Tables are in use and can't or can only with a lot of work be changed.

I am trying to get a Php/Html based Tool to work with the DB.

Here is my Info:

try {
    $conn = new PDO("sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
} catch (PDOException $e) {
    die("Error connecting to SQL Server " . $e);
}

This is my PDO connection and

select * from munition.dbo.V_Admin_Zündhütchen order by Kaliber desc

this my query.

V_Admin_Zündhütchen is a View.

When i fetch the return and dump it like this:

$row = $sql->fetch(PDO::FETCH_ASSOC)
var_dump($row);

I get this:

array (size=5)
  'ID' => string '2' (length=1)
  'Kaliber' => string '7,62 mm' (length=7)
  'Hersteller' => string 'Vihtavuori' (length=10)
  'Z�ndtyp' => string 'Boxer' (length=5)
  'Bezeichnung' => string '1511' (length=4)

the � is supposed to be a ü

The byte sequence of what is shown as Z�ndtyp is 5A FC 6E 64 74 79 70

The Data itself is fine only the Columnames are affected:

array (size=5)
  'ID' => string '24' (length=2)
  'Kaliber' => string '12/70' (length=5)
  'Hersteller' => string 'Rottweil' (length=8)
  'Z�ndtyp' => string 'Boxer' (length=5)
  'Bezeichnung' => string 'Schrotzünder' (length=13)

So why does it not resolve properly?

VolkerK
  • 95,432
  • 20
  • 163
  • 226
Jens Krüger
  • 263
  • 1
  • 3
  • 18
  • 1
    Could this be related to https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a9196e8e-4ce5-451b-a2c5-b7a3bf276fda/pdosqlsrvencodingsystem-pdosqlsrvencodingutf8-apparently-not-working?forum=sqldriverforphp ? – VolkerK Sep 14 '15 at 09:15
  • Yes it is related but i'am allready running the latest Version(3.2) of the driver. So that should not pose a problem! – Jens Krüger Sep 14 '15 at 09:27
  • Just to be absolutely sure about the encoding please run `foreach( $row as $k=>$v) { if ( 'Z'===$k[0] ) { $foo=$foo=str_split($k); array_walk( $foo, function($e) { printf('%02X ', ord($e)); }); } }` instead of the var_dump. It's supposed to show the byte sequence of the key `Zündtyp`; please add the output to your question text. – VolkerK Sep 14 '15 at 09:47
  • Not sure if this will help but while you're at it: Could you please also post the table definition; and/or the collation information as described here: http://stackoverflow.com/questions/7321159/determining-the-character-set-of-a-table-database ? – VolkerK Sep 14 '15 at 09:52
  • 5A FC 6E 64 74 79 70 ==> Output Latin1_General_CI_AS ==> DatabseCollation that could be the problem – Jens Krüger Sep 14 '15 at 09:52
  • ...so much for adding this information to the question text ;-) – VolkerK Sep 14 '15 at 09:52
  • Ok, the `ü` really is encoded as the single byte `FC` which is the latin1/cp-1252/... encoding. Does this also happen when an Umlaut is in the record itself (not in the field name as in your example)? (sorry, to bother you with all these questions. The answer right now is: I don't know; I can only help you to narrow it down.) – VolkerK Sep 14 '15 at 09:55
  • yeah sometime reading is hard... my bad – Jens Krüger Sep 14 '15 at 09:59
  • "Latin1_General_CI_AS ==> DatabseCollation that could be the problem" - maybe it is. I have to admit I've never tried field ids outside the ascii7 charset with t-sql/sqlserver. But right now I have only my tablet as a windows machine -> can't even fire up a localdb to test it ;-) – VolkerK Sep 14 '15 at 10:02
  • Since the data is correct and the encoding is to... i think it is a problem with the translation to the array! Must be an error when coding it the columnames – Jens Krüger Sep 14 '15 at 10:28
  • Yeah, that's what I suspect, too. Maybe it's a known problem ...known to people who actutally work with sql server (instead of just fiddeling around with it like me ;-)). In your place I'd also add the versions of a) the sql server and b) the native client used to your question. ...and maybe also ask the good people over at technet/msdn ;-) – VolkerK Sep 14 '15 at 10:31

0 Answers0