Fatal error: Call to undefined function sqlsrv_connect()
Asked Answered
A

8

40

I have come across quite a few post regarding the same subject question, however I am still unable to resolve it and so I ask. I am trying to connect to sql in my php script. My connection string is:

/* Specify the server and connection string attributes. */
$serverName = "xxx-PC\SQLExpress";
$connectionOptions = array("Database"=>"Salesforce");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
      die(print_r(sqlsrv_errors(), true));
}

I have installed and included the following in my php.ini file located under the wamp folder: C:\wamp\bin\php\php5.4.16:

extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll

My wampserver is running fine and so are the wampapache and wampsqld services. I am able to execute php.exe successfully. However I am unable to make the connection to SQL Server 2008 R2 where my database is located. Please help!

EDIT 1: The wamp server is running the wampmysql service while I am trying to connect to SQL Server 2008 R2. Could this be the reason? Should I be using MySQL instead of SQL? Any pointers?

EDIT 2: I do not see sqlsrv section at all when I run phpinfo() though I have added extension=php_sqlsrv_54_ts.dll in the php.ini file located in the bin folder of the wamp server.

enter image description here

Anapest answered 25/2, 2014 at 13:6 Comment(2)
To avoid red herrings: MySQL and SQL Server are completely different database engines—the code you've posted does not use neither PDO nor MySQL.Eastward
@ÁlvaroG.Vicario I removed the PDO dll and updated the path to the extensions added so as to make sure it refers the dll in the php/ext folder. However, my phpinfo() return no sqlsrv section. Any pointers?Anapest
A
12

This helped me get to my answer. There are two php.ini files located, in my case, for wamp. One is under the php folder and the other one is in the C:\wamp\bin\apache\Apachex.x.x\bin folder. When connecting to SQL through sqlsrv_connect function, we are referring to the php.ini file in the apache folder. Add the following (as per your version) to this file:

extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll
Anapest answered 26/2, 2014 at 14:1 Comment(6)
Do you add this under Windows extension in the php.ini file ?Cyndy
@user2995165 yes this is under windows extensionAnapest
I did that but the error is persistant. Does it make any difference if the database is sitting in a different server ? Because I have two servers, an application server with wamp and database server with SQL server 2005.Cyndy
No that should not be a problem. What is the error message you are getting?Anapest
can you have a look at my post: #24776578 . I couldn't get any help. It will super awesome if you can help me figure it out. I am getting Fatal error: Call to undefined function sqlsrv_connect()Cyndy
This was my issue; I was editing the wrong config. Thank You! I wanted to add that the config file being used is listed in a phpinfo(); output dump, so always be sure to check.Faculty
H
20

When you install third-party extensions you need to make sure that all the compilation parameters match:

  • PHP version
  • Architecture (32/64 bits)
  • Compiler (VC9, VC10, VC11...)
  • Thread safety

Common glitches includes:

  • Editing the wrong php.ini file (that's typical with bundles); the right path is shown in phpinfo().
  • Forgetting to restart Apache.
  • Not being able to see the startup errors; those should show up in Apache logs, but you can also use the command line to diagnose it, e.g.:

    php -d display_startup_errors=1 -d error_reporting=-1 -d display_errors -c "C:\Path\To\php.ini" -m
    

If everything's right you should see sqlsrv in the command output and/or phpinfo() (depending on what SAPI you're configuring):

[PHP Modules]
bcmath
calendar
Core
[...]
SPL
sqlsrv
standard
[...]

phpinfo()

Holden answered 26/2, 2014 at 11:32 Comment(4)
I modified the php again and saw the sqlsrv in the command output. However, I do not see it when I run phpinfo(). It does not contain any headers for sqlsrv. Considering I do see it at the command output, does that indicate my settings are right?Anapest
php -i is the same as phpinfo(), save for the format (plain text vs HTML).Eastward
I added a screen shot above. If its the same why is sqlsrv not reflected in the html page? I still get the same error when I try to conenct to my SQL Server using sqlsrv_connect.Anapest
solved! The issue was I was referring to the wrong php.ini file - embarrassed.Anapest
A
12

This helped me get to my answer. There are two php.ini files located, in my case, for wamp. One is under the php folder and the other one is in the C:\wamp\bin\apache\Apachex.x.x\bin folder. When connecting to SQL through sqlsrv_connect function, we are referring to the php.ini file in the apache folder. Add the following (as per your version) to this file:

extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll
Anapest answered 26/2, 2014 at 14:1 Comment(6)
Do you add this under Windows extension in the php.ini file ?Cyndy
@user2995165 yes this is under windows extensionAnapest
I did that but the error is persistant. Does it make any difference if the database is sitting in a different server ? Because I have two servers, an application server with wamp and database server with SQL server 2005.Cyndy
No that should not be a problem. What is the error message you are getting?Anapest
can you have a look at my post: #24776578 . I couldn't get any help. It will super awesome if you can help me figure it out. I am getting Fatal error: Call to undefined function sqlsrv_connect()Cyndy
This was my issue; I was editing the wrong config. Thank You! I wanted to add that the config file being used is listed in a phpinfo(); output dump, so always be sure to check.Faculty
R
6

If you are using Microsoft Drivers 3.1, 3.0, and 2.0. Please check your PHP version already install with IIS.

Use this script to check the php version:

<?php echo phpinfo(); ?>

OR

If you have installed PHP Manager in IIS using web platform Installer you can check the version from it.

Then:
If you are using new PHP version (5.6) please download Drivers from here

For PHP version Lower than 5.6 - please download Drivers from here

  • PHP Driver version 3.1 requires PHP 5.4.32, or PHP 5.5.16, or later.
  • PHP Driver version 3.0 requires PHP 5.3.0 or later. If possible, use PHP 5.3.6, or later.
  • PHP Driver version 2.0 driver works with PHP 5.2.4 or later, but not with PHP 5.4. If possible, use PHP 5.2.13, or later.

Then use the PHP Manager to add that downloaded drivers into php config file.You can do it as shown below (browse the files and press OK). Then Restart the IIS Server

enter image description here

If this method not work please change the php version and try to run your php script. enter image description here

Tip:Change the php version to lower and try to understand what happened.then you can download relevant drivers.

Rollin answered 4/3, 2015 at 12:35 Comment(0)
P
1

First check that the extension is properly loaded in phpinfo(); (something like sqlsrv should appear). If not, the extension isn't properly loaded. You also need to restart apache after installing an extension.

Paternity answered 25/2, 2014 at 13:11 Comment(4)
@DaveChild I ran phpinfo() and do not see sqlsrv. Am I missing it? Where should it appear? And how to upload the extension properly if that is indeed the issue?Anapest
If you're missing it (which is most likely), then the issue is the loading of the extension. There are plenty of possible reasons it's not loaded. Start by checking you modified the right php.ini file (the php.ini file location will be shown in phpinfo). Check your error logs for more info as well.Paternity
@DaveChild I am sorry but this is my first time with sql in php. Under what headers can I check the path of php.ini? I am completely lost with this.Anapest
@DaveChild please see the edit in the question above.Anapest
R
1

i have same this because in httpd.conf in apache PHPIniDir D:/wamp/bin/php/php5.5.12 that was incorrect

Remorseful answered 28/1, 2015 at 6:32 Comment(0)
M
1

Search for sqlsrv extension in pdo_sqlsrv.

Monarch answered 8/8, 2021 at 5:23 Comment(0)
M
1

Got it fixed. Even though I specified the folder that everything was stored, in my case C:\shared it turns out that PHP was just looking in c:\ for the lumina-online-dependencies folder.

I copied it to C:\ and it now works

Maidinwaiting answered 6/4, 2022 at 10:12 Comment(0)
G
1

I'm using MAMP and PHP 8.1 on Windows 10. I needed to download the SQL Server drivers from Microsoft (https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16), and then extract and copy the proper driver dll in this folder:

C:\MAMP\bin\php\php8.1.0\ext

(Side note: Laravel uses PDO.)

And then reference it:

In this file: C:\MAMP\conf\php8.1.0\php.ini

Add this line: extension=php_pdo_sqlsrv_81_ts_x64.dll

And in this file: C:\MAMP\bin\php\php8.1.0\php.ini

Add this line: extension=pdo_sqlsrv_81_ts_x64

And of course restart the services / Apache.

Then I could connect like this:

try {
    $conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
        array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );


    $sql = "SELECT * FROM Table";

    foreach ($conn->query($sql) as $row) {
        print_r($row);
    } 


} catch (PDOException $e) {
    echo ("Error connecting to SQL Server: " . $e->getMessage());
}

Hope that helps someone else!

Gadson answered 25/7, 2022 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.