Home >PHP Framework >Laravel >How does laravel5 connect to sqlserver through freetds (code)

How does laravel5 connect to sqlserver through freetds (code)

不言
不言Original
2018-07-31 16:22:443161browse

This article introduces to you about laravel5 connecting to sqlserver through freetds. It has certain reference value. I hope it can help friends in need.

Related versions

The system is ubuntu 16.04, the PHP version used is 7.0.30, sqlserver 2012, freetds is 0.92 Laravel5.5 and 5.4 have been tested

Install php driver

sudo apt-get install php7.0-odbc 
sudo apt install php7.0-sybase

Install freetds

sudo apt-get install freetds-bin freetds-common tdsodbc odbcinst unixodbc unixodbc-dev  
sudo mv /etc/odbcinst.ini /etc/odbcinst.ini.bak  
sudo cp /usr/share/tdsodbc/odbcinst.ini /etc/

Configure freetds

 sudo vim /etc/freetds/freetds.conf

Modify configuration

[global]
    tds version = 8.0 # TDS version, ref <a href="http://www.freetds.org/userguide/choosingtdsprotocol.htm" target="_blank">this</a>.
    client charset = UTF-8
    text size = 20971520
[Server2012] #自定义名称,后面需要使用
    host = {yourdomain}.database.windows.net // ip地址或域名
    port = 1433
    tds version = 8.0 #8.0为2012其他自行测试

Test SQLSERVER

 TDSVER=8.0 tsql -H my_server_host -p 1433 -U my_user -P my_password -D my_database

Configure Laravel5

Openconfig/database.phpAdd configuration in connections, the driver uses sqlsrv

   'mssql' => [
        'driver' => 'sqlsrv',
        'host' => 'Server2012', // 这个对应freetds.conf的配置名称
        'port' => '1433',
        'database' => env('DB_DATABASE', '数据库'),
        'username' => env('DB_USERNAME', '用户'),
        'password' => env('DB_PASSWORD', '密码'),
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],

Multiple databases

If you use mysql I also want to use some information of sqlserver for personal project reasons, but the general approach is to write an API for the sqlserver system to be called by the Mysql system, but this time I was lazy and used both together
Add ## to the Model #protected $connection = 'mssql';And use protected $table = 'EMPLOYEE'; Specify the data table, so you don't need to write the connection in the Controller every time.

Related Recommendation:

Implementation of laravel framework in data statistical drawing

Using process of Echo in Laravel framework

The above is the detailed content of How does laravel5 connect to sqlserver through freetds (code). For more information, please follow other related articles on the PHP Chinese website!

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