2

I want to connect to a specific database on SQL Server Through PHP. On WAMP Server, I have PHP 5.6 installed so I downloaded SQLSRV32.EXE (Microsoft Drivers for PHP for SQL Server) then I copied these files in C:\wamp64\bin\php\php5.6.25\ext enter image description here

IN php.ini, I added

extension=php_sqlsrv_56_nts.dll

extension=php_sqlsrv_56_ts.dll

But when i check phpinfo() on browser i don't see the sqlsrv module listed.

When i try to connect to my SQL Server Database I get this error

Fatal error: Call to undefined function sqlsrv_connect()

Here is my code

<?php
$serverName = "PC0CFEP2\SQLEXPRESS"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

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

I was checking my solutions on different forums and I tried many but I still can't get it work. I am sure it's in some settings but i couldn't find out what could be the issue.

Any suggestion please ? Thank you very much.

3
  • Whatever initializes sqlsrv_connect() it's not loading and that's why you can't call that function. Commented Jul 26, 2017 at 16:12
  • 1
    You only need the Thread Safe extension in WAMPServer so thats php_sqlsrv_56_ts.dll The Non Thread Safe version wont run and will cause you issues Commented Jul 26, 2017 at 16:15
  • @RiggsFolly i removed the non thread safe version but still the same issue Commented Jul 26, 2017 at 18:51

2 Answers 2

2

At First, Please run MSSQL server database.

In php.ini file, Please added below code

extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll

and keep two dll file \ext folder

To connect database , try

$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer. I have done the same but still not working. The sqlsrv is still not showing on the php.info too. is there any other setting i need to change in the php.ini ?
2

Remove "php_" prefix:

extension=sqlsrv_56_ts.dll
extension=pdo_sqlsrv_56_ts.dll

This worked for me. Thanks for asking this question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.