0

我在centos 服务器上使用microsoft sql server 作为数据库和我的php 代码。使用 freetds 和 dblib 从 yii 框架连接到 mssql。

一切都很好。通过存储过程插入数据库后,数据保存但在数据库查询中我们有??????? 在此 NVARCHAR 列中。

我的 utf-8 阿拉伯语数据。这是我的配置和代码:

fretds.conf

[mss]  
        host = 172.31.1.2  
        ip = 172.31.1.2  
        port = 1433  
        tds version = 7.0

yii主要配置

'db'=>array(
        'connectionString' => 'dblib:host=mss;database=XXXX;charset=utf8',
        'username' => 'XXX',
        'password' => 'XXXXXXXX',
        'charset' => 'utf8',
        'tablePrefix' => 'tbl_',
        'enableProfiling' => true,
        'schemaCachingDuration' => 5 * 60 * 60,
        ),

我的模型存储过程

$builder=$this->getCommandBuilder();
$table=$this->getMetaData()->tableSchema;

$command=$builder->createSqlCommand('EXEC dbo.sp_link_comment_insert  :link_id, :cmnt_parent_id, :user_id, :cmnt_status, :cmnt_text, :cmnt_thread',
                    array(
                        ':link_id'=>58829,
                        ':cmnt_parent_id'=>'',
                        ':user_id'=>9,
                        ':cmnt_status'=>1,
                        ':cmnt_text'=>'تست ارسال comment',
                        ':cmnt_thread'=>0,
                    )
                );
$command->execute();
4

2 回答 2

1

迟到的回复。尝试使用选项在 freetds 配置中指定编码client charset = UTF-8。更改后您的配置文件将如下所示:

[mss]  
    host = 172.31.1.2  
    ip = 172.31.1.2  
    port = 1433  
    tds version = 7.0
    client charset = UTF-8

根据文档,无法识别的字符被转换为?.

如果 FreeTDS 遇到无法转换的字符,则其行为会根据问题的严重程度而有所不同。在从服务器检索数据时,FreeTDS 替换为 ASCII '?' 在字符的位置,并发出一条警告消息,指出某些字符无法转换。

参考: http: //freetds.schemamania.org/userguide/localization.htm

于 2012-10-12T06:46:46.160 回答
0

Your driver (freeTDS) does not support UTF-8 for MSSQL. The only driver that natively supports UTF-8 for MSSQL is SQLDRV driver available only for windows platform.

To store data in UTF-8 using any other than SQLDRV driver (SQLDRV is not available for linux at the moment), the application must take care of converting from source charset into UTF-8 and vice versa. Therefore, setting "charset=UTF8" in your connection string has no effect.

In PHP, you should convert data before storing into MSSQL like so:

$dataUTF8 = iconv('windows-1250', 'utf-8', $data);
$sql = "INSERT INTO table (value) VALUES ($dataUTF8)";

and convert them back when reading from MSSQL:

$sql = "SELECT dataUTF8 FROM table;";
$data = iconv('utf-8', 'windows-1250', $dataUTF8);
于 2014-08-04T16:32:38.613 回答