PHP 7.x connection with MSSQL server with MAMP
Asked Answered
R

4

5

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.

Reverso answered 6/10, 2016 at 7:21 Comment(1)
Are you hosting on linux or windows?Corkwood
G
10

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.

Guberniya answered 28/8, 2018 at 22:1 Comment(2)
I got two errors trying this, first solved by /Applications/MAMP/bin/php/php7.x.x/bin/pecl channel-update pecl.php.net (Replacing x.x with version number) Then I got a make error and solved that by brew install unixodbcSanderson
thanks! your comment helped me figure my issue. In the most voted answer the pecl command is called using path so it did not install the extensions for the php version I was using for the site that needed it. Calling pecl like that allowed me to install it on the version I needed!Diplococcus
B
5

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.

Baptlsta answered 25/1, 2019 at 21:8 Comment(2)
Thanks for this! For me, the brew tap that worked was brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release, brew update and then brew install msodbcsql17 mssql-toolsJocularity
I also had pecl install sqlsrv pdo_sqlsrv fail with autom4te: need GNU m4 1.4 or later, which was solved by following this post: superuser.com/questions/668746/… (with a slight alteration)Jocularity
A
4
  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.

Antipope answered 14/12, 2017 at 1:24 Comment(0)
W
0

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

Waverly answered 14/8, 2019 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.