首页 > php教程 > php手册 > 正文

在linux或mac上symfony如何连接sqlserver

php中文网
发布: 2016-06-06 20:12:42
原创
1357人浏览过

microsoft provides a?sql server?driver?for pdo. unfortunately, this driver only works on windows. linux and mac os x apps must use the?freetds?compatibility layer:?an open source implementation of the ms sql server protocol for unix. It’s

microsoft provides a?sql server?driver?for pdo. unfortunately, this driver only works on windows. linux and mac os x apps must use the?freetds?compatibility layer:?an open source implementation of the ms sql server protocol for unix.

It’s possible to connect?a Symfony app to a SQL Server instance on Unix through FreeTDS but this involve to use a Doctrine driver that is not provided with the standard distribution. Some tutorials already explain how to do that, but they encourage doing dirty things like editing files in the?vendor/?directory. Here is the clean way!

First, install FreeTDS.

On Mac OX X, use?Homebrew:
brew install freetds

On Debian or Ubuntu:
apt-get install freetds-bin

The next step is to configure FreeTDS to be able to connect to the SQL Server instance.

Open the?freetds.conf?file (/etc/freetds/freetds.conf?on Debian / Ubuntu and/usr/local/etc/freetds.conf?on Mac OS X) and add the connection details of your server:

[my_server]<br> host = sql.example.com<br> port = 1433<br> tds version = 8.0<br> client charset = UTF-8<br> text size = 20971520

Be sure to set the protocol version to 8.0, the client charset and the text size.

Now, you should be able to connect to the SQL server from the command line:
tsql -S my_server -U myusername

Type your password when asked and?Ctrl+D?to disconnect from the server.

It’s time to install the?DBLIB PDO Driver.

On Mac OS X (replace php55 by the version of PHP you are using):
brew install?php55-pdo-dblib

腾讯云AI代码助手
腾讯云AI代码助手

基于混元代码大模型的AI辅助编码工具

腾讯云AI代码助手 205
查看详情 腾讯云AI代码助手

On Debian or Ubuntu:
apt-get install php5-sybase

And add the DBLIB driver for Doctrine (packaged in?a Symfony bundle) in your app:

# in your Symfony app directory<br> composer require?realestateconz/mssql-bundle:dev-master

Enable the Symfony bundle. Add this line in the?registerBundles()?method of your?AppKernelin?app/AppKernel.php:

new Realestate\MssqlBundle\RealestateMssqlBundle(),

Finally, configure Doctrine to use this driver. Edit?app/config/config.yml:

doctrine:

????dbal:

????????driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver

????????host: my_server

????????dbname: MYDATABASE

????????user: myuser

????????password: mypassword

Note that you must use the?driver_class?parameter, and not?driver. Of course, you should not hardcode these values. Use?the interactive parameters system?instead.

Your Symfony app is now able to connect to the SQL Server. Try to run a SQL query:
php app/console doctrine:query:sql "SELECT * FROM MY_TABLE"

As SQL Server is a bad default DBMS for a Symfony app, especially on Unix servers, you should be interested by?using multiple database connection with Symfony and Doctrine.

另外注意你的编码,sqlserver insert需要将你的utf8转成gb2312

Like this :

$data = array_map(function($value) { return iconv(“UTF-8″,”GB2312″,$value); }, $data);

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号