PHP 7.x connection with MSSQL server with MAMP

老子叫甜甜 提交于 2019-12-05 00:44:49

问题


I am trying to connect mssql server to PHP 7.0.8 through MAMP. I have tried using freetds. On some blog people are saying to use pdo_dblib.so extension but it's not working.

Please guide me through the process of connection.


回答1:


  1. Follow this guide through step 3: Microsoft PHP drivers for SQL Server
  2. Find where pecl drops extensions in your local machine
  3. Copy the files pdo_sqlsrv.so and sqlsrv.so into your MAMP's PHP extension directory. Mine was located at /Applications/MAMP/bin/php/php7x.x/lib/php/extensions/no-debug-foo-bar
  4. Edit your php.ini file to include the new extensions:

    extension=sqlsrv.so
    extension=pdo_sqlsrv.so
    
  5. Restart your MAMP servers.




回答2:


For those who still have this problem:

/Applications/MAMP/bin/php/php7.2.1/bin/pecl install sqlsrv pdo_sqlsrv

Edit php.ini:

extension=sqlsrv.so
extension=pdo_sqlsrv.so

If necessary, use brew install autoconf if you don't have it already.




回答3:


While the answers posted by Vague Space and Pedro Santiago helped, I still think the answers are a bit lacking and incomplete… Or ask you to do too much. Honestly the official Microsoft instructions are overkill when they state you need to install their Docker image of SQL Server and such? C’mon… Most people just need the drivers installed to make a connection.

So here is my answer based on my experience installing the pdo_sqlsrv.so and sqlsrv.so modules in MAMP (version 5.2) but should work for most any MAMP version that supports some flavor of PHP 7.

Adjust the .bash_profile so MAMP’s binaries and libraries are a part of your $PATH settings.

First, adjust your .bash_profile so the MAMP stuff is in there; makes it easier to launch and work with MAMP specific binaries and ensures MAMP libraries are checked when doing things like installing new modules like this.

The way I like to do it is like this; set $MAMP_BIN and $MAMP_PHP variables like this and then rebuild the $PATH variables:

# MAMP stuff.
export MAMP_BIN="/Applications/MAMP/Library/bin";
export MAMP_PHP="/Applications/MAMP/bin/php/php7.2.10/bin";

# Final $PATH setting.
export PATH="/usr/local/bin:/usr/local/sbin:$MAMP_BIN:$MAMP_PHP:$PATH";

Save it and just log out of the Terminal session and back in, or just resource the .bash_profile like this:

source ~/.bash_profile

With that done, let’s install the core Microsoft ODBC binary stuff.

Install the Microsoft ODBC stuff.

Do this to install the core ODBC stuff on macOS; be sure to have Homebrew installed:

brew tap microsoft/SQLSRV-release https://github.com/Microsoft/homebrew-SQLSRV-release
brew update
brew install --no-sandbox msodbcsql17 SQLSRV-tools

Then when that’s done, go ahead and install the Unix ODBC stuff like this:

brew install unixodbc

Now install the actual PHP modules via PECL:

pecl install sqlsrv pdo_sqlsrv

With the modules installed, add them to the php.ini file in MAMP so PHP can recognize it. For PHP 7.2.10 on MAMP 5.x it should be located here:

/Applications/MAMP/bin/php/php7.2.10/conf/php.ini

And just add these config lines to the bottom of the file:

; Enable 'Microsoft Drivers for PHP for SQL Server' extension module
extension = sqlsrv.so
extension = pdo_sqlsrv.so

; Configuration

;sqlsrv.WarningsReturnAsErrors = 1
;sqlsrv.LogSeverity = 0
;sqlsrv.LogSubsystems = 0
;sqlsrv.ClientBufferMaxKBSize = 10240

;pdo_sqlsrv.log_severity = 0
;pdo_sqlsrv.client_buffer_max_kb_size = 10240

Note, most tutorials—and even PECL when you install the modules—simply mention adding extension = sqlsrv.so and extension = pdo_sqlsrv.so to the php.ini config, but these config options are the ones that RedHat has when installing the PHP SQLSRV via the Remi repo. Yeah, most of them are commented out but I still like having it there for reference.




回答4:


having just done this in 2019 with MAMPPRO4 on windows 10 (follow upto step 4 to test that you are connected and then do point 9 ) point 5 onwards is for changing the path in the command line

  1. download dll files from microsoft https://www.microsoft.com/en-gb/download/details.aspx?id=20098
  2. follow the instruction after running the exe file and place the dll files into the extension directory of the php version that you are using eg: MAMP/bin/php/php7.1.29/ext
  3. check phpinfo for the Loaded Configuration File of the php.ini file
  4. add the 2 dll files depending on your requirements (I wasted time by using the 64.dll) make sure you are using ts(thread safe) not nts(none thread safe) in the file name of the dll extension=php_sqlsrv_71_ts_x86.dll extension=php_pdo_sqlsrv_71_ts_x86.dll
  5. in control panel search for advanced system settings and click
  6. click Environment Variables
  7. under system variables not user variables click path and click edit
  8. click new and add C:\MAMP\bin\php\php7.1.29 (Edit this to your path)
  9. restart MAMP
  10. open a new command line an enter php -v you should see the php version displayed


来源:https://stackoverflow.com/questions/39889897/php-7-x-connection-with-mssql-server-with-mamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!