Call to undefined function sqlsrv_connect() - Troubleshooting

Benjamin Löffel picture Benjamin Löffel · Jun 21, 2017 · Viewed 13.3k times · Source

System Information

CMS: Wordpress

Web Server: XAMPP

PHP Version: 5.5.30

MS Management Studio 17

Goal

Establish MSSQL Database connection using PHP

What has been done

  1. Downloaded SQLSRV Drivers
  2. Copied files php_pdo_sqlsrv_55_nts.dll and php_pdo_sqlsrv_55_ts.dll to the directory C:\xampp\php\ext
  3. Added the following lines to the dynamic extensions part in the php.ini file: extension=php_pdo_sqlsrv_55_ts.dll and extension=php_pdo_sqlsrv_55_nts.dll
  4. Restarted Web Server
  5. Confirmed sqlsrv is listed in phpinfo()

Code

$serverName = "technology-pc\sqlexpress";

// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"example_db");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
} else {
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

Error

Call to undefined function sqlsrv_connect()

Answer

ImClarky picture ImClarky · Jun 21, 2017

You have added the PDO variant of SQLSRV drivers to the extension list, but have not added the base drivers php_sqlsrv_55_ts.dll.

Add to the php.ini:

extension=php_sqlsrv_55_ts.dll

or

extension=php_sqlsrv_55_nts.dll

Also, you really should be using either the Thread-Safe (_ts.dll) or Non-Thread-Safe (_nts.dll) versions of the driver, not both. I believe that, as you are using an Apache Server, you should be using the Thread-Safe versions. So you php.ini should have:

extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll