php / php/php-src

unicode string corruption when using PDO_ODBC with FreeTDS 1.3.6 and MS SQL Server 2016

Ouverte
#9,498 10 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Bug Extension: pdo_odbc Status: Needs Triage
Langage dominant
C
Étoiles
40.4k
Forks
8.1k
Merge moyen
2 j 13 h
PR mergées (30 j)
96

Description

Description

The following code:

<?php
// setup database connection
$u = getenv('DB_USERNAME');
$p = getenv('DB_PASSWORD');
$dsn = 'odbc:testUTF8';
$db = new \PDO($dsn, $u, $p);
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$constant_tests = [
    "SELECT CAST(0x72006f017e016f007600fd00 AS nvarchar(10)) AS pink_in_czech_binary_encoded_utf16;",
    "SELECT CAST(0x6b26 AS nvarchar(3)) AS Beamed_Eighth_Notes_binary_encoded_utf16;",
    "SELECT NCHAR(0x266b) AS Beamed_Eighth_Notes_nchar;",
    "SELECT N'♫' AS Beamed_Eighth_Notes_unicode_string_constant;",
    "SELECT N'růžový' AS pink_in_czech_unicode_string_constant;",
];
foreach($constant_tests as $t) {
    fwrite(STDERR, "Running query '$t'...\n");
    var_dump($db->query($t, \PDO::FETCH_ASSOC)->fetchAll());
}

Resulted in this output:

Running query 'SELECT CAST(0x72006f017e016f007600fd00 AS nvarchar(10)) AS pink_in_czech_binary_encoded_utf16;'...
array(1) {
  [0]=>
  array(1) {
    ["pink_in_czech_binary_encoded_utf16"]=>
    string(9) "růžový"
  }
}
Running query 'SELECT CAST(0x6b26 AS nvarchar(3)) AS Beamed_Eighth_Notes_binary_encoded_utf16;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_binary_encoded_utf16"]=>
    string(3) "♫"
  }
}
Running query 'SELECT NCHAR(0x266b) AS Beamed_Eighth_Notes_nchar;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_nchar"]=>
    string(3) "▒▒"
  }
}
Running query 'SELECT N'♫' AS Beamed_Eighth_Notes_unicode_string_constant;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_unicode_string_constant"]=>
    string(3) "▒▒"
  }
}
Running query 'SELECT N'růžový' AS pink_in_czech_unicode_string_constant;'...
array(1) {
  [0]=>
  array(1) {
    ["pink_in_czech_unicode_string_constant"]=>
    string(9) "růžo"
  }
}

But I expected this output instead:

Running query 'SELECT CAST(0x72006f017e016f007600fd00 AS nvarchar(10)) AS pink_in_czech_binary_encoded_utf16;'...
array(1) {
  [0]=>
  array(1) {
    ["pink_in_czech_binary_encoded_utf16"]=>
    string(9) "růžový"
  }
}
Running query 'SELECT CAST(0x6b26 AS nvarchar(3)) AS Beamed_Eighth_Notes_binary_encoded_utf16;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_binary_encoded_utf16"]=>
    string(3) "♫"
  }
}
Running query 'SELECT NCHAR(0x266b) AS Beamed_Eighth_Notes_nchar;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_nchar"]=>
    string(3) "♫"
  }
}
Running query 'SELECT N'♫' AS Beamed_Eighth_Notes_unicode_string_constant;'...
array(1) {
  [0]=>
  array(1) {
    ["Beamed_Eighth_Notes_unicode_string_constant"]=>
    string(3) "♫"
  }
}
Running query 'SELECT N'růžový' AS pink_in_czech_unicode_string_constant;'...
array(1) {
  [0]=>
  array(1) {
    ["pink_in_czech_unicode_string_constant"]=>
    string(9) "růžový"
  }
}
PHP Version

PHP 8.1.7

Operating System

Debian GNU/Linux testing/bookworm/unstable

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le problème avec le script PHP fourni, PDO::query() et fetchAll() via PDO_ODBC avec FreeTDS 1.3.6 et SQL Server 2016. Comparez les valeurs renvoyées pour NCHAR et les constantes de chaîne préfixées par N avec les chaînes Unicode attendues ; le travail est terminé lorsque toutes les requêtes listées renvoient les caractères attendus sans corruption.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
php, sql
Domaine
databases
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.