PHP undefined function mssql_connect and sqlsrv_connect

Posted by Damodar Bashyal on April 06, 2016

 

mssql_connect is now deprecated - http://php.net/manual/en/function.mssql-connect.php now move to sqlsrv_connect - http://php.net/manual/en/function.sqlsrv-connect.php

ERROR: exception 'Exception' with message 'connection to mssql is not supported. Please download and follow directions from here - https://www.microsoft.com/en-us/download/details.aspx?id=20098' in C:\_projects\technooze\app\code\local\Technooze\Schoolbrochure\Model\Mssql.php:182

For sqlsrv_connect to work download (Microsoft Drivers for PHP for SQL Server) from https://www.microsoft.com/en-us/download/details.aspx?id=20098

I downloaded - SQLSRV32.EXE (I have 32 bits windows and it worked for me.)

Install downloaded SQLSRV32.EXE and set to extract at 'extension_dir' check in your phpinfo(). Mine is: C:\Program Files\Ampps\php\ext.

If you have a AMPPS installation like me, you can select new dlls from PHP Extensions manager. Or, just add below lines manually in your php.ini. For me ampps added at the bottom of the page like this:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_ts.dll

After restarting apache server it worked for me.

 

UPDATE 1:

Also you may need to install Microsoft ODBC Driver from - https://msdn.microsoft.com/en-us/library/cc296170.aspx if you get this error:

This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712

I downloaded "1033\x86\msodbcsql.msi" from (https://www.microsoft.com/en-us/download/details.aspx?id=36434)

 

UPDATE 2:

For my new 64 bit windows machine I had to download SQL Server 2014 Express with Advanced Services 64 Bit instead, as I was unable to install ODBC driver only:
- https://www.microsoft.com/en-au/server-cloud/products/sql-server-editions/sql-server-express.aspx

Restart your computer and disable/manual all sql server services as below otherwise when you start your ampps/xampp/wamp etc you will get "Port 80 is being used by SYSTEM (PID 4)" error.

windows services

 

NOTE:

If it didn't work for you, make sure you selected the right version.

I have PHP version 5.5.x and Thread Safety = "enabled" (check in phpinfo). So I used php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll

Previously I selected 54 dll and I got below error, so if you choose right one you won't get error when you do "php -v" in cmd.

Error:

PHP undefined function mssql_connect and sqlsrv_connect

 

PHP Warning:  PHP Startup: sqlsrv: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20121212
These options need to match
 in Unknown on line 0

Warning: PHP Startup: sqlsrv: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20121212
These options need to match
 in Unknown on line 0

PHP Warning:  PHP Startup: pdo_sqlsrv: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20121212
These options need to match
 in Unknown on line 0

Warning: PHP Startup: pdo_sqlsrv: Unable to initialize module
Module compiled with module API=20100525
PHP    compiled with module API=20121212
These options need to match
 in Unknown on line 0

PHP 5.5.31 (cli) (built: Jan  6 2016 12:57:50)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with the ionCube PHP Loader v5.0.17, Copyright (c) 2002-2015, by ionCube Ltd.
 
not published on website


QR Code: PHP undefined function mssql_connect and sqlsrv_connect