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.
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
sudo apt-get install php7.0-odbc sudo apt install php7.0-sybase
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/
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其他自行测试
TDSVER=8.0 tsql -H my_server_host -p 1433 -U my_user -P my_password -D my_database
Openconfig/database.php
Add 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, ],
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.
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!