Rick Roy
Rick Roy

Reputation: 1728

Connecting to microsoft sql server using php

I am trying to connect to a mssql server using php. The sql server is on a different machine in the network and I have XAMPP installed in my machine. I don't have microsoft sql installed in my server.

I have downloaded the sqlsrv drivers for PHP and then in my php.ini file added the extension extension=php_pdo_sqlsrv_55_ts.dll under windows extension.

Added the php_pdo_sqlsrv_55_ts.dll file inside the php\ext\ folder of my XAMPP installation.

After restarting apache phpinfo(); shows that the sqlsrv driver is enabled

PDO support enabled
PDO drivers mysql, sqlite, sqlsrv

This is my code

<?php
$serverName = "192.168.100.102, 1433";
$connectionInfo = array( "Database"=>"ATP", "UID"=>"raihan", "PWD"=>"temp123#");
$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));
}
?>

But on executing I get this error

Fatal error: Call to undefined function sqlsrv_connect() in D:\xampp\htdocs\database\index.php on line 4

Why this error? And how do I go about connecting to mssql server using php?

Upvotes: 1

Views: 1948

Answers (1)

Korvo
Korvo

Reputation: 9714

php\etc\ sounds like wrong folder, try put in php\ext\ folder

Note extension=php_pdo_sqlsrv_55_ts.dll is used by PDO class

  1. Go to https://www.microsoft.com/en-us/download/details.aspx?id=20098
  2. Select download by your PHP version:

    • Select SQLSRV30.EXE if use SQL Server 2005
    • Select SQLSRV31.EXE or SQLSRV31.EXE if use SQL Server 2008
  3. After download, extract dlls
  4. If you use php with ThreadSafe (apache2handler -- more probable) copy php_sqlsrv_55_ts.dll fot ext folder

For use sqlsrv uncomment (or put) this line in php.ini extension=php_sqlsrv_55_ts.dll (or extension=php_sqlsrv_55_nts.dll for no-thread-safe)

Upvotes: 2

Related Questions