Score:0

PHP unable to identify sqlsrv_connect() function while trying to connect SQL Server

cn flag

I'm trying to connect to my local MSSQL Server from a simple PHP file using the sqlsrv_connect() function, but every time I'm calling the file in the browser through localhost, it's throwing a 500 (Internal Server Error) saying: "PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\inetpub\wwwroot\AJAX_Tutorial\get_db_data.php:4". get_db_data.php is the file from which I'm trying to connect the server. Seems like PHP or the localhost can't identify the sqlsrv_connect() function. But as far I'm concerned, I did all the needful to make sure PHP connects the SQL Server.

My environment: Windows 10 Pro, Version 21H2, 64-bit.

What I have done:

  1. Enabled IIS ensuring CGI/Fast CGI is working.
  2. Installed PHP 8.1.1 non-thread safe x64 version at C:\Program Files\PHP-8.1.1
  3. In C:\Program Files\PHP-8.1.1, renamed php.ini-development file to php.ini
  4. In the php.ini file, uncommented the extension_dir = "ext" directive.
  5. Downloaded php_wincache.dll and added it to the default ext directory of PHP.
  6. Added the line extension=php_wincache.dll at the Dynamic Extensions section of the php.ini file.
  7. Installed PHPManagerForIIS_V1.5.0 and configured IIS accordingly so that PHP can be hosted through IIS. Also enabled the php_wincache.dll extension here.
  8. Installed MSSQL Server 2019 Developer Edition along with the respective Management Studio.
  9. Created the respective database and tables in SQL Server that I want to connect to from PHP.
  10. Ensured Microsoft ODBC Driver 17 for SQL Server is installed in my PC, that is required by PHP.
  11. Ensured Microsoft SQL Server 2012 Native Client is installed in my PC, that is required by PHP.
  12. Downloaded Microsoft Drivers for PHP for SQL Server 5.9 and extracted its contents. Copied the file named php_sqlsrv_80_nts_x64.dll in the package and pasted it in the default ext directory of PHP.
  13. Added the line extension=php_sqlsrv_80_nts_x64.dll at the Dynamic Extensions section of the php.ini file.
  14. In IIS Manager, through PHP manager, enabled the php_sqlsrv_80_nts_x64.dll extension.
  15. Created a phpinfo.php file in the root of the IIS, which ran successfully but found no mention of wincache and sqlsrvin it.

After the steps above, I ran the actual PHP file trying to connect the SQL Server, but it's throwing an error saying it can't identify the sqlsrv_connect() function. Assuming the php_sqlsrv_80_nts_x64.dll not being loaded while PHP is starting, I ran php --ini in the command prompt. That's when the following messages are being thrown:

PHP Warning: PHP Startup: Unable to load dynamic library 'php_wincache.dll' (tried: ext\php_wincache.dll (The specified module could not be found), ext\php_php_wincache.dll.dll (The specified module could not be found)) in Unknown on line 0

Warning: PHP Startup: sqlsrv: Unable to initialize module Module compiled with module API=20200930 PHP compiled with module API=20210902 These options need to match in Unknown on line 0 Configuration File (php.ini) Path: Loaded Configuration File: C:\Program Files\PHP-8.1.1\php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none)

However, PHP seems to be running fine, because when I used jQuerey AJAX get() and post() method from an HTML file to fetch data from another PHP file, I was successfull in doing so. No exception was thrown then.

So what am I missing now that neither php_wincache.dll and sqlsrv seem to load during PHP startup, nor can I connect the SQL Server from the PHP file? As I'm new in jQuery AJAX and PHP, I'm not much aware of the intricacies of them and hence, stuck with the issue for the past four days. I've used every resource in my hand, but nothing is working. Please help. I can't get ahead with my tasks because of this.

Thanks and Regards!

get_db_data.php code:

<?php
    $serverName = "(local)";    // Optionally use port number (1433 by default).
    $connectionString = array("Database"=>"TestDB");    // Connection string.
    $conn = sqlsrv_connect($serverName, $connectionString); // Connect using Windows Authentication.

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

    sqlsrv_close($conn);    // Close connection resources.
?>
jp flag
You have different API versions
cn flag
Different API versions of what? As far as PHP and MS-SQL Server documentations are concerned, I'm using PHP 8.1.1 which contains php8.dll and the respective SQL Server Driver, i.e., SQLSRV59. Here's the link for PHP https://windows.php.net/download#php-8.1 and here's the link for SQL Server Driver recommended for PHP 8.0 https://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?redirectedfrom=MSDN&view=sql-server-ver15. The only difference is I'm using PHP 8.1.1.
cn flag
But if you visit the link for SQL Server Driver, in the section "Microsoft Drivers 5.9 for PHP for SQL Server", scroll down to PHP version 8.0. There, it's written the driver dll can be used with 64-bit php8.dll, and that's exactly what my PHP 8.1.1 uses, i.e., php8.dll. That's why I downloaded this driver. So what do you suggest me to do now? Delete my current installation of PHP 8.1.1 and get back to PHP 8.0?
cn flag
Moreover, the error warning you quoted above is also shown for the "missing" php_wincache.dll that is actually present in the ext directory of PHP. So why is it throwing the "unable to load dynamic library" error for php_wincache.dll too? Any suggestion, please?
jp flag
It is unable to load the module because it doesn’t match API version
Score:2
jp flag

You have PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() because sqlsrv module wasn't loaded. The module wasn't loaded because it is compiled with module API=20200930 while your PHP compiled with module API=20210902 as your error message clearly says. PHP expects modules to be compiled with the same API version as the PHP itself. You need to either get PHP the same version as the module was compiled for or get a module compiled for your PHP version.

cn flag
You were right, version mismatch was the problem. While I was using PHP-8.1, I downloaded the SQLSRV for PHP-8.0. Because of this mismatch the driver file wasn't loading during the start of the PHP in the server. Downgrading to PHP-8.0 solved the issue. Thank you for pointing out the crux of the trouble.
Score:1
in flag

I think https://github.com/microsoft/msphpsql/releases/download/v5.10.0-beta2/Windows-8.1.zip is what you need.

try these in your php.ini

extension=php_pdo_sqlsrv_81_ts.dll
extension=php_sqlsrv_81_ts.dll
cn flag
Version mismatch was the problem. While I was using PHP-8.1, the SQLSRV driver was for PHP-8.0, because of which PHP couldn't load the driver during starting up. Thanks for sharing the link for SQLSRV for PHP-8.1. Couldn't find it in the official Microsoft page.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.