Getting data with UTF-8 charset from MSSQL server using PHP FreeTDS extension

前端 未结 8 922
暖寄归人
暖寄归人 2020-11-28 09:28

I can\'t seem to get data from MSSQL encoded as UTF-8 using FreeTDS extension.

Connecting:

ini_set(\'mssql.charset\', \'UTF-8\');
$this->_resource         


        
相关标签:
8条回答
  • 2020-11-28 10:13

    I had a similar problem and tried all settings i could find on the web - in vain.

    In my case the problem was the configuration of FreeTDS itself. On Linux the file is /etc/freetds/freetds.conf

    I had to change the Version to 7.0 (maybe other numbers work, too. i just tried 7.0)

    [global]
        # TDS protocol version
        tds version = 7.0
    

    After this, the driver seemed to accept changes of the charset like.

    ini_set('mssql.charset', 'UTF-8');
    

    Btw: the change immediately is in effect, no need to restart anything afterwards

    0 讨论(0)
  • 2020-11-28 10:18

    It seem version 7.0 or great is required. iconv() also seems to work well, but is tedious.

    $query = $this->db->query($sql);
    $result = $query->fetchAll(PDO::FETCH_OBJ);
    foreach ($result as $row) {
        foreach (get_object_vars($row) as $key => $value) {
        $row->$key = (mb_detect_encoding($value, mb_detect_order(), true) === 'UTF-8') 
                ? $value : iconv('iso-8859-1', 'utf-8', $value);
        }
        $results[] = $row;
    }
    
    0 讨论(0)
提交回复
热议问题