How to use SQL Server connection in Laravel?

后端 未结 4 1508
悲&欢浪女
悲&欢浪女 2020-12-03 05:01

I got a working project made in Laravel 3 that I have to switch to MsSQL Server (not my call though, sniff...) and I don\'t understand the Laravel configuration on this data

4条回答
  •  天命终不由人
    2020-12-03 05:40

    That's what I did to connect my Laravel app to MS SQL Server:

    1. sudo apt-get update

    2. sudo apt-get install php5-sybase

    if you try to install php5-mssql it will install php5-sybase anyway, it is “Sybase / MS SQL Server module for php5”.

    this will make Laravel use the dblib driver instead of sqlsrv.

    check out this file for reference Illuminate\Database\Connectors\SqlServerConnector.php.

    1. open this file: /etc/freetds/freetds.conf

    under [global] section and under tds version = 4.2

    add tds version = 8.0            (without ; semi column at the beginning of the line)

    and add client charset = UTF-8 just under the above line            (without ; semi column at the beginning of the line)

    This lets the driver encode all the data in utf-8 and avoid the strange characters in data

    the file will look like this:

    [global]
    
            # TDS protocol version
    
    ;       tds version = 4.2
    
            tds version = 8.0
    
            client charset = UTF-8
    
    1. create file locales.conf in this directory /etc/freetds and past the following inside it:     (this allow correct parsing of dates with SQL Server).

    [default]
    
        date format = %Y-%m-%d %I:%M:%S.%z
    
    [en_US]
    
        date format = %b %e %Y %I:%M:%S:%z%p
    
        language = us_english
    
        charset = iso_1
    

    1. edit the php.ini file

    first find the file with php -i | grep php.ini

    then open the file and search for mssql.charset       (it will probably be like disabled like this         ; mssql.charset = "ISO-8859-1"     )

    make it mssql.charset = "UTF-8"   <>

    save and exit

    1. now restart your server (nginx or apache or php-fpm)

    2. of course you know that you need to set the database config to use sqlsrv by default 'default' => 'sqlsrv', and add your credentials.


    OTHER METHODS:

    you can use ODBC driver to connect

    here’s a nice package https://github.com/ccovey/odbc-driver

提交回复
热议问题