Table of Contents
Related versions" >Related versions
Install php driver" >Install php driver
Install freetds" >Install freetds
Configure freetds" >Configure freetds
Test SQLSERVER" >Test SQLSERVER
Configure Laravel5" >Configure Laravel5
Multiple databases" >Multiple databases
Home PHP Framework Laravel How does laravel5 connect to sqlserver through freetds (code)

How does laravel5 connect to sqlserver through freetds (code)

Jul 31, 2018 pm 04:22 PM
laravel php7 sql sqlserver

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

Install php driver

sudo apt-get install php7.0-odbc 
sudo apt install php7.0-sybase
Copy after login

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/
Copy after login

Configure freetds

 sudo vim /etc/freetds/freetds.conf
Copy after login

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其他自行测试
Copy after login

Test SQLSERVER

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

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,
    ],
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Laravel - Artisan Console Laravel - Artisan Console Aug 27, 2024 am 10:51 AM

Laravel - Artisan Console - Laravel framework provides three primary tools for interaction through command-line namely: Artisan, Ticker and REPL. This chapter explains about Artisan in detail.

Laravel - Pagination Customizations Laravel - Pagination Customizations Aug 27, 2024 am 10:51 AM

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? How to make PHP5.6 and PHP7 coexist through Nginx configuration on the same server? Apr 01, 2025 pm 03:15 PM

Running multiple PHP versions simultaneously in the same system is a common requirement, especially when different projects depend on different versions of PHP. How to be on the same...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

See all articles