Home  >  Article  >  Backend Development  >  yii2 - linux、mac下,php连接sql server的问题

yii2 - linux、mac下,php连接sql server的问题

WBOY
WBOYOriginal
2016-06-06 20:36:201242browse

我们有个项目,由不同团队开发。一边用windows + .net + sql server 2008;另外一边用linux + yii(php) + mysql开发,现在想用yii去连接sql server,开发环境的系统是osx,怎么解决连接的问题?

回复内容:

我们有个项目,由不同团队开发。一边用windows + .net + sql server 2008;另外一边用linux + yii(php) + mysql开发,现在想用yii去连接sql server,开发环境的系统是osx,怎么解决连接的问题?

http://www.yiiframework.com/doc/guide/1.1/en/database.dao#establishing...

和链接mysql是一样的。
你去连接sql server的话,把dsn配置改下就可以了

<code>$connection=new CDbConnection('mssql:host=localhost;dbname=testdb',$username,$password);
</code>

如果host不是在一台机器上就该下host=sql_server_host

找到一些答案,遇到一些问题做个记录。

http://php.net/manual/zh/pdo.drivers.php

目前采用freeTDS和pdo_dblib解决的,但是编码有问题。yii用utf-8,sql server上用gbk。

osx配置如下,utf8放下面会报错,不知道什么原因。

<code>'api' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'dblib:host=x.x.x.x;dbname=dbname;charset=utf8',
        'username' => 'username',
        'password' => 'password',
        //'charset' => 'utf8',
    ],
</code>

ubuntu配置如下,utf8放哪一样,程序运行正常,但是写入sql server会乱码。

<code>    'api' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'dblib:host=x.x.x.x;dbname=dbname;charset=utf8',
        'username' => 'username',
        'password' => 'password',
        //'charset' => 'utf8',
    ],
</code>

编码的问题已经解决,看这里:http://segmentfault.com/q/1010000002661362

研究问题之后,知道了。 unix/linux 连接 sql server 的方式很多种,比如 freetds + unixODBC(iODBC)等。

php的pdo能支持,不管哪种数据库,无非是数据库驱动器和数据库管理器的连接的问题。

PHP的PDO链接:http://php.net/manual/zh/pdo.drivers.php

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn