Problems with encoding SQL Server 2008

Asked

Viewed 160 times

0

I am performing a query in an Sql Server database, but the special characters are returned as follows:

MAR�O

instead of

MARÇO

I was able to fix using the function utf8_encode(), but I do not want to have to keep treating each result, and performing some research I understood that it is possible to make some configuration in the connection so that the information already come with encoding correct, but I haven’t been able to apply it yet. Follow my connection class for better viewing:

<?php

class pdo_dblib_mssql {

    private $db;
    private $cTransID;
    private $hostname;
    private $port;
    private $dbname;
    private $username;
    private $pwd;
    private $table;
    private $columns;
    private $where = array();
    private $childTrans = array();

    public function __construct($data, $table) {
        $this->hostname = $data['host'];
        $this->port = $data['port'];
        $this->dbname = $data['schema'];
        $this->username = $data['user'];
        $this->pwd = $data['password'];

        $this->table = $table;

        $this->connect();
    }

    public function connect() {
        try {
            $this->db = new PDO("dblib:host=$this->hostname:$this->port;dbname=$this->dbname", "$this->username", "$this->pwd");
        } catch (PDOException $e) {
            $this->logsys .= "\n Failed to get DB handle: " . $e->getMessage();
        }
    }

    public function beginTransaction() {

        $cAlphanum = "AaBbCc0Dd1EeF2fG3gH4hI5iJ6jK7kLlM8mN9nOoPpQqRrSsTtUuVvWwXxYyZz";
        $this->cTransID = "T" . substr(str_shuffle($cAlphanum), 0, 7);

        array_unshift($this->childTrans, $this->cTransID);

        $stmt = $this->db->prepare("BEGIN TRAN [$this->cTransID];");
        return $stmt->execute();
    }

    public function rollBack() {

        while (count($this->childTrans) > 0) {
            $cTmp = array_shift($this->childTrans);
            $stmt = $this->db->prepare("ROLLBACK TRAN [$cTmp];");
            $stmt->execute();
        }

        return $stmt;
    }

    public function commit() {

        while (count($this->childTrans) > 0) {
            $cTmp = array_shift($this->childTrans);
            $stmt = $this->db->prepare("COMMIT TRAN [$cTmp];");
            $stmt->execute();
        }

        return $stmt;
    }

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

    public function select($columns){
        $this->columns = $columns;

        return $this;
    }

    public function where($column, $value, $op = "=") {
        $this->where[] = "$column $op '$value'";
        return $this;
    }

    public function get(){
        $sql = "SELECT " . implode(", ", $this->columns) . " FROM $this->table WHERE " . implode(" AND ", $this->where);
        $stmt = $this->db->prepare($sql);
        $stmt->execute();
        $ret = $stmt->fetchAll();

        var_dump($ret);die;
    }
}

Note: This database is third party, which provides me with a view for consultation, so larger seat-side settings are not possible.

The collation of view is Latin1_General_CI_AS

It is possible to automatically treat these characters in some way by performing some configuration on the connection or before performing the query?

  • related: http://answall.com/questions/115042/pdo-dblib-e-codificacao-utf-8

1 answer

0

Tried to?

setAttribute( PDO::SQLSRV_ENCODING_UTF8 ); 
  • returned the following error: Fatal error: Undefined class constant 'SQLSRV_ENCODING_UTF8'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.