0

I'm using PDO ODBC Driver 13 with Linux Apache, everything works correctly except for the following: If I sample the rows in a table using fetch and if the word has special characters, it removes the last character from the word. For example: The word 'Maça' gives me the result 'Maç', but if I write 'maçaa' the result is as expected, 'maça'

My settings are as follows:

Class DatabaseODBC{
 
    private $server = "odbc:Driver={ODBC Driver 13 for SQL Server};Server=--, 1433;Database=--;Server_CSet=UTF-8;Client_CSet=UTF-8;";
    private $username = "--";
    private $password = "--";
    private $options  = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
    protected $conn;
    
    public function open(){
        try{
            $this->conn = new PDO($this->server, $this->username, $this->password, $this->options);
            return $this->conn;
        }
        catch (PDOException $e){
            echo "Problem with connection " . $e->getMessage();
        }
 
    }

    public function close(){
        $this->conn = null;
    }
 
}

The index page is:

$stmt = $connODBC->prepare("SELECT 'MAÇA' as text, name FROM users(nolock) WHERE id = :id");
$stmt->execute(['id' => 1]);
foreach ($stmt as $row) {
    echo $row["text"];
}
  • Just for curiosity, can you dump the result of `$row["text"]` as a [byte array](https://stackoverflow.com/a/11466734/231316) to confirm that it isn't a display/render problem? – Chris Haas Aug 18 '20 at 12:45
  • string(5) "Maç", to solution the problem i change the select with `CAST('Maça' AS CHAR(5))` and solve the problem, have ideia where is the problem? – Marco Machdo Aug 18 '20 at 12:50
  • Hi Marco, that isn't the byte array, just the `__toString()` representation. The byte array will be a bunch of hex strings or ints. – Chris Haas Aug 18 '20 at 12:56
  • @ChrisHaas sorry, is that you need: `array(5) { [1]=> int(77) [2]=> int(97) [3]=> int(195) [4]=> int(167) [5]=> int(0) } ` ? – Marco Machdo Aug 18 '20 at 12:59
  • Thanks Marco. 77 = `M`, 97 = `a` (lowercase), 195 & 167 = `ç` (lowercase). First, can you confirm that you provided `Maça` and not `MAÇA`? That last 0 in the byte array is troublesome, that's a null byte which could be a string terminator, or it could be a glitch. Instead of `text` for the type, can you try casting to `ntext` or `nvarchar`? – Chris Haas Aug 18 '20 at 13:18
  • @ChrisHaas thank you, if i casting to ntext the byte array is: `array(5) { [1]=> int(77) [2]=> int(97) [3]=> int(195) [4]=> int(167) [5]=> int(111) }`and vardump `string(5) "Maço"` – Marco Machdo Aug 18 '20 at 15:24

0 Answers0