Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()

19,626

Conform to the list of your tries in php.ini (php_sqlsrv_XX_ts.dll), you have activated extensions for PHP v5.5 and v5.6. But, since you are running PHP 7.0, you need to download and activate the corresponding Microsoft Drivers for PHP for SQL Server. It seems that you need to download the driver version 4.0. This is stated on the php.net page for SQLSRV extension.

The SQLSRV extension is supported by Microsoft and available for download here: http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx.

Here you can find the instructions for Loading the PHP SQL Driver. You'll probably receive a file like php_sqlsrv_7_XX.dll with XX: "ts" or "nts" (thread safe or non-thread safe). I think you need the thread safe version.

Otherwise, use MS SQL Server (PDO) PDO driver (read the "Installation" part in the link!) with the PDO_SQLSRV DSN (connection string) and the PDO general functionality, e.g. common to all drivers. The driver will be added to the php.ini as extension=php_pdo_sqlsrv_7_ts.dll then (check the same page for instructions: Loading the PHP SQL Driver). If you ask me, I would really choose this option.

Nota Bene: Don't forget to restart the web server and/or the db service after each change in configs!

Good luck.

Share:
19,626

Related videos on Youtube

WolfieeifloW
Author by

WolfieeifloW

Updated on June 04, 2022

Comments

  • WolfieeifloW
    WolfieeifloW almost 2 years

    I am trying to connect to a SQL Server that we can access through "Microsoft SQL Server Management Studio":

    enter image description here

    I've tried a bunch of different things I've seen on SO but nothing has worked. I get the error Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\Apache24\htdocs\test.php:52 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\test.php on line 52 when running the following code on the test.php page:

    $serverName = "SERVER.DOMAIN.COM, 1433"; //serverName\instanceName, portNumber (default is 1433)
    $connectionInfo = array( "Database"=>"DB_NAME", "UID"=>"USER", "PWD"=>"PASS");
    $ma_conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    if( $ma_conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    

    My php.ini file has gone through many different extensions being enabled, but none have worked. I've tried all of the following:

    extension = php_sqlsrv_56_ts.dll
    extension=php_sqlsrv_55_ts.dll
    extension=php_sqlsrv_55_nts.dll
    extension=php_sqlsrv_55_ts.dll
    extension=php_pdo_sqlsrv_55_ts.dll
    extension=php_pdo_mssql.dll
    extension=php_pdo_mysql.dll
    

    Currently I have:

    extension_dir = "c:\apache24\php7\ext\"
    extension=php_pdo.dll
    extension=php_pgsql.dll
    [PHP_SQLSRV]
    extension=php_sqlsrv_54_ts.dll
    [PHP_PDO_SQLSRV]
    extension=php_pdo_sqlsrv_54_ts.dll
    

    On the machine with Apache and PHP in CMD I get the following:

    Microsoft Windows [Version 6.3.9600]
    (c) 2013 Microsoft Corporation. All rights reserved.
    
    C:\Windows\system32>php -c c:\apache24\php7\php.ini -v -display_startup_errors=1
    
    PHP Warning:  PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_pdo.dll' - %1 is not a valid Win32 application.
     in Unknown on line 0
    PHP Warning:  PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application.
     in Unknown on line 0 PHP
    Warning:  PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_pdo_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application.
     in Unknown on line 0
    PHP 7.0.10 (cli) (built: Aug 18 2016 09:48:53) ( ZTS )
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    
    C:\Windows\system32>
    

    I've also downloaded and ran SQLSRV32.EXE inside of the C:\Apache24\php7\ext folder.

    I have:

    • Windows Server 2012 RS win64
    • Apache 2.4.23 win64
    • PHP 7.0.10 win64
    • Microsoft SQL Server 2012 Native Client win64
    • Microsoft Visual C++ 2012 x64 & x86 Redistributables (two different)
    • Microsoft Visual C++ 2015 Redistributables (x64 & x86) - 14.0.23026
    • Microsoft ODBC Driver 11 for SQL Server

    enter image description here

    enter image description here