2 votes

Fatal error: Call to undefined function sqlsrv_connect()

I think many of you have seen the following error:

Fatal error: Call to undefined function sqlsrv_connect()

I have done everything, changed versions, read many forums and nothing. I have the following extensions enabled in the php.ini

extension=php_pdo_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_ts.dll

In the error.log nothing related comes out, only the error itself:

[Wed Mar 15 18:18:35.602697 2017] [:error] [pid 8080:tid 1728] [client ::1:49291] PHP Fatal error: Call to undefined function sqlserv_connect() in...

As you can see, the phpinfo.php does not look bad. Let's see if someone can give me a hand. I would be immensely grateful.

introducir la descripción de la imagen aquí

1 votes

In... Can you show the code snippet where you get that error and what database driver you are using (MySQL, SQL Server or other)?

0 votes

Don't you have a sqlsrv section in your phpinfo? I mean, because what you show is the pdo_sqlsrv extension and it's not the same.

1voto

Jorny Points 434

WHAT HAPPENED TO ME
I used Microsoft drivers and it did not work.

HOW I SOLVED IT
I downloaded the "Unofficial" drivers from
http://robsphp.blogspot.pe/2012/06/unofficial-microsoft-sql-server-driver.html
2. In the zip, there are the drivers for PHP 32 and 64 bits.

HOW TO INSTALL THEM (VERY IMPORTANT TO READ WELL)

  1. Copy the DLL's to the PHP ext folder.

    • The DLLs must be the same version with PHP installed.
    • The DLLs must match PHP if it is TS or NTS.
  2. In PHP.INI add the following lines at the end of the extensions section:

    • extension=php_sqlsrv_56_ts.dll
    • extension=php_pdo_sqlsrv_56_ts.dll

    NOTE: The name of the DLL file must be the one for which it is being installed, in this example it is for PHP 5.6 TS.

  3. Save PHP.INI and restart apache service

NOTE: I am using SQL Server 2016 SP1 and it is working normally, Good luck.

0 votes

Hi, how are you? we are doing the same steps but it does not even appear in the phpinfo. Can you give us a hand? we have the latest XAMPP with PHP Version 7.1.6

0 votes

@BraianMellor, it doesn't work with php 7.1, although there is a way to "make it work", we could exchange the information by a more active and functional way.

0 votes

Please. Tell me how you want to do it, we are really stuck. Thanks

0voto

I had a similar problem, I was using xampp v3.2.4 with php version 7.4.11 and the unofficial drivers are for php 5 to 5.5. I tried this one and it worked for me

extension=php_pdo_sqlsrv_74_ts_x86.dll

for SQLSRV 3.0. for php 7 onwards, I tried this one and it worked for me. https://go.microsoft.com/fwlink/?linkid=2120362 SQLSRV 2.0. support for PHP 5.2 and/or PHP compiled with VC6.

-1voto

RO Nerio Points 1

It is not necessarily a matter of whether the drivers are signed or not, what usually happens is that we do not download the correct driver.

For this Witchcraft to work you need to check the PHP version you have, in your case 5.6, therefore you must download from the official microsoft site the correct library. In the case of PHP 5.6 you must download the microsoft dirver that has the termination 32, for PHP 7 version you must use the library with termination 40.

Then follow the instructions in this manual http://php.net/manual/es/ref.pdo-sqlsrv.php

and to make the connection what worked for me is the following example:

//declare the function that performs the connection to the BDD

function OpenConnection()  
{  
    try  
    {  
        $serverName = "nombre de la instancia"; 
        $connectionOptions = array("Database"=>"nombre de la BDD",  
            "Uid"=>"usuario del SQL", "PWD"=>"El PAsword"); 
        $conn = sqlsrv_connect($serverName, $connectionOptions);  
        return $conn;
        /*
          //en mi caso no reconoce la función FormatErrors() por eso lo 
            comenté
        if($conn == false)  
            die(FormatErrors(sqlsrv_errors()));  
        */
    }  
    catch(Exception $e)  
    {  
        echo("Error!");  
    }  
}  
//Esta función genera ejecuta una consulta cualquiera
    function ReadData($tsql)  
{  
    try  
    {  
        $conn = OpenConnection();  
        $tsql ;  
        $getNames = sqlsrv_query($conn, $tsql);  
        /*if ($getNames == FALSE)  
            die(FormatErrors(sqlsrv_errors()));  */
        $productCount = 0;  
        while($row = sqlsrv_fetch_array($getNames, SQLSRV_FETCH_ASSOC))  
        {  
            echo($row['nombre']);  
            echo("<br/>");  
            $productCount++;  
        }  
        sqlsrv_free_stmt($getNames);  
        sqlsrv_close($conn); 

    }  
    catch(Exception $e)  
    {  
        echo("Error!");  
    }  
} 

//here we send to call the function placing the query as parameter

ReadData("SELECT algo FROM algunaTabla")  ;

I hope someone finds it useful, it took me days to understand the new way of connecting to SQL SERVER from PHP.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X