Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP IIS7 MSSQL Call to undefined function sqlsrv_connect

I have MSSQL Server 2008, MS WINDOWS SERVER 2008 RC2 AND PHP 5.4

php.info have rows

[PHP_PDO_SQLSRV_54_NTS]
extension=php_pdo_sqlsrv_54_nts.dll
[PHP_PDO_ODBC]
extension=php_pdo_odbc.dll
[PHP_SYBASE_CT]
extension=php_sybase_ct.dll
[PHP_SQLSRV_54_NTS]
extension=php_sqlsrv_54_nts.dll

Php info found here http://89.111.180.28/index.php

i see lines with Extensions php.ini but i dont see lines with extensions SQLSRV in php.info...

for connect to MSSQL SERVER i use script

$conn_array = array (
            "UID" => "user", 
            "PWD" => "passw", 
            "Database" => "database",
            "Encrypt" => 1,
            "TrustServerCertificate" => 1) ;

$conn2 = sqlsrv_connect('localhost' , $conn_array);

if ($conn2)
    {
        echo 'MSSQL Connection successful';
    }
else
    {
        die( print_r( sqlsrv_errors(), true));
    }

sqlsrv_close( $conn2 );

But i see error:

Fatal error: Call to undefined function sqlsrv_connect() in C:\inetpub\wwwroot\89.111.180.28\index.php on line 18

Tell me please why i have error and how make the right ?

p.s.: server already was restarted.

like image 787
Leo Loki Avatar asked Dec 04 '12 13:12

Leo Loki


1 Answers

I just had this problem myself. I finally got it fixed, so I figured I'd share.


The problem was that, although I had the sqlsrv dll installed (copied to my php/ext folder), and I had it added in my php.ini, in IIS, it was 'disabled'.

Here's some step by step instructions, in case anyone has this same problem again. (Or for future reference for me :))

  1. Download (and install) the SQL Server drivers (.dll)

    • Install them by running the .exe, and typing the path to your php extensions folder when it asks you where to decompress them.
      • To find your current extension directory, run (cmd.exe) php -i | more, and look for the line extension_dir. (For me it was on the fourth press of more). Alternately, make a simple php file containing only <?php phpinfo(); ?>, and run it in the browser. This will give the same information, but in a much easier-to-read format.
  2. Add the extension to your php.ini

    • To find the right php.ini, either run php -i | more again, looking for Loaded Configuration File, or check that simple php script again (I highly recommend you make it - it'll save you time and effort). The path you find there is the file you need to edit.
    • Add the following lines to your php.ini, and save it:

      [PHP_SQLSRV] extention=php_sqlsrv_56_nts.dll

  3. Enable the extension in IIS Manager

    • In the start menu, type IIS Manager, and press enter.
    • Click on your Server's name in the left side bar
    • Click PHP Manager
    • Under PHP Extensions, click Enable or Disable an Extension.
    • If your extension is not under Enabled, look under Disabled for it. When you find it, right-click on it, and click Enable in the context menu that appears.
  4. Test to make sure it worked

    • Open that phpinfo() page you made (you did, didn't you?), and look under Registered PHP Streams. If you see sqlsrv in that list, you're set!
like image 116
Cullub Avatar answered Sep 19 '22 09:09

Cullub