PHP + SQL Server - How to set charset for connection?

前端 未结 13 1002
借酒劲吻你
借酒劲吻你 2020-12-01 06:54

I\'m trying to store some data in a SQL Server database through php.

Problem is that special chars aren\'t converted properly. My app\'s charset is iso-8859-1 and th

相关标签:
13条回答
  • 2020-12-01 06:58

    Client charset is necessary but not sufficient:

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

    I searched for two days how to insert UTF-8 data (from web forms) into MSSQL 2008 through PHP. I read everywhere that you can't, you need to convert to UCS2 first (like cypher's solution recommends). On Windows SQLSRV said to be a good solution, which I couldn't try, since I am developing on Mac OSX.

    However, FreeTDS manual (what PHP mssql uses on OSX) says to add a letter "N" before the opening quote:

    mssql_query("INSERT INTO table (nvarcharField) VALUES (N'űáúőűá球最大的采购批发平台')", +xon);
    

    According to this discussion, N character tells the server to convert to Unicode. https://softwareengineering.stackexchange.com/questions/155859/why-do-we-need-to-put-n-before-strings-in-microsoft-sql-server

    0 讨论(0)
  • 2020-12-01 06:59

    For me editing this file:
    /etc/freetds/freetds.conf
    ...and changing/setting 'tds version' parameter to '7.0' helped. Edit your freetds.conf and try to change this parameter for your server configuration (or global).

    It will work even without apache restart.

    0 讨论(0)
  • 2020-12-01 07:05

    I did not notice someone to mention another way of converting results from MSSQL database. The good old iconv() function:

    iconv (string $in_charset, string $out_charset, string $str): string;
    

    In my case everything else failed to provide meaningful conversion, except this one when getting the results. Of course, this is done inside the loop of parsing the results of the query - from CP1251 to UTF-8:

    foreach ($records as $row=>$col) {
        $array[$row]['StatusName'] = iconv ('CP1251', 'UTF-8' , $records[$row]['StatusName']);
    }
    

    Ugly, but it works.

    0 讨论(0)
  • 2020-12-01 07:05

    You should set the charset with ini_set('mssql.charset', 'windows-1252') before the connection. If you use it after the mssql_connect it has no effect.

    0 讨论(0)
  • 2020-12-01 07:05

    Just adding ini_set('mssql.charset', 'UTF-8'); didn't help me in my case. I had to specify the UTF-8 character set on the column:

    $age = 30;  
    $name = utf8_encode("Joe");    
    
    $select = sqlsrv_query($conn, "SELECT * FROM Users WHERE Age = ? AND Name = ?",
        array(array($age), array($name, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STRING('UTF-8')));
    
    0 讨论(0)
  • 2020-12-01 07:08

    I suggest looking at the following points:

    1. Ensure that the columns that you're storing the information in are nchar or nvarchar as char and nvarchar don't support UCS-2 (SQLServer doesn't store in UTF-8 format btw)
    2. If you're connecting with the mssql library/extension for PHP, run: ini_set('mssql.charset', 'utf-8'); as there's no function with a charset argument (connect, query etc)
    3. Ensure that your browsers charset is also set to UTF-8
    0 讨论(0)
提交回复
热议问题