5

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.

Leo Loki
  • 109
  • 1
  • 1
  • 6

4 Answers4

4

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!
Cullub
  • 2,901
  • 3
  • 30
  • 47
  • If you have to change anything to make it work, leave a comment saying what you did, to be of help to later people. – Cullub Jan 26 '16 at 12:55
  • 1
    Note: I also had to install [Microsoft® ODBC Driver 11](https://www.microsoft.com/en-us/download/details.aspx?id=36434). – Cullub Jan 26 '16 at 14:06
  • 1
    I have php 7.2 and followed your instructions. I can see in php manager that the extensions php_sqlsrv_72_ts_x64 and php_pdo_sqlsrv_72_ts_x64 are enabled. But phpinfo() does not show sqlsrv under registered PHP streams, what should I check? – Ching Liu Sep 28 '18 at 16:30
3

Your answer definitely helped me debug my problem. To add on to answer this question for others who may have successfully installed the PHP PDO SQLSRV .dll module but still get the same error.

It took me almost an hour to figure this out, but even after I configured PHP_PDO_SQLSRV, the function sqlsrv_connect() still didn't work.

Found out it's because if you are using the PDO sqlsrv extension, it has its own library with separate functions.

To connect to the database, you would do something like this:

$conn = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");

You can view all of them here:

http://msdn.microsoft.com/en-us/library/ff628159(v=sql.105).aspx

If you are unsure of the differences between PDO and normal SQLSRV, please refer to this post:

SQLSRV driver vs. PDO driver for PHP with MS SQL Server

I chose PDO because I prefer data to be returned as strings (faster access for me instead of parsing the object/class types).

Community
  • 1
  • 1
TrieuNomad
  • 1,651
  • 1
  • 21
  • 24
0

I understand the situation.

I used PHP version above 5.4, so the use of PHP driver for Windows 3.0.

For this configuration, required client version of SQL 2012, but i have been the client SQL 2008 version

Problem solved.

Leo Loki
  • 109
  • 1
  • 1
  • 6
0

all you need to do is to download missed components like database manager and other like microsoft sql server 2008R2 management objects if you use sql server 2008 r2 and keep downloading till you get successed

  • Please take a moment to clarify your answer by referencing a package or php SQL plugin required to help resolve the issue. Or if you have direct knowledge of the error message expanding on the error's definition. – Mark Carpenter Jr Feb 28 '20 at 20:37