0

I'm new to PHP. PHP is installed and working, and now I'm trying to set up connection with a SQL Server DB. Following instructions from a Youtube video. Went through and finished installing the drivers, but it isn't working. My test page stops running code after the sqlsrv_connect() function is called. I'm not even getting an error message. Below is my test page code:

<?php

echo '<br><span style="font-weight:bold;">PHP functioning. This line is being echoed out.</span><br />';
/*No problems yet...*/

$serverName = "Myserver";
$connectionInfo = array('Database'=>'nde', 'UID'=>'sa', 'PWD'=>'!hegEcu&3ATr');
$conn = sqlsrv_connect($serverName, $connectionInfo);
/*Nothing is appearing on the screen after this */

echo 'Still working...<br />';


if ($conn) {
    echo 'Connection established.<br />';
} else {
    echo 'Nope.<br />';
    die(print_r(sqlserv_errors(), TRUE));
}

?>

I'm running PHP 7.0.21, and SQL Server 2014. I went and downloaded the two DLL files I think I need - php_pdo_sqlsrv_7_ts_x64.dll, and php_sqlsrv_7_ts_x64.dll. I copied them to Program Files/PHP/v7.0/ext. I went into PHP.ini as admin, and typed: extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
in the Windows Extensions sections. I saved PHP.ini, restarted both the IIS Admin Service and World Wide Web Publishing Service, and went into IIS Manager and restarted the server. PHP is still working, but I'm still not getting any echo after the sqlsrv_connect() call.

Cmaso
  • 897
  • 1
  • 7
  • 17
  • 1
    Use nts versions of the dll. Also, since you have the PDO extension installed, use PDO instead of `sqlsrv_connect` – ctwheels Aug 16 '17 at 16:53
  • [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). But the PHP extension is not a standalone solution, you still need the appropriate drivers by Microsoft. – Álvaro González Aug 16 '17 at 17:05
  • Why use the not threadsafe versions? And why use PHP Data Objects instead of sqlsrv_connect()? – Cmaso Aug 16 '17 at 17:17

1 Answers1

0

Okay, I got it working. As ctwheels suggested, i just installed the nts driver versions instead of the ts versions, which are php_pdo_sqlsrv_7_nts_x64.dll and extension=php_sqlsrv_7_nts_x64.dll. It's working with sqlsrv_connect(); I haven't tried using PDO. I'm new to PHP and unfamiliar with PDO, but I'll probably come across it again, especially as I start looking into PHP frameworks (Cake and CodeIgniter).

One important thing to share for anyone who might also be new to PHP - the PHP documentation says that the $serverName argument of sqlsrv_connect() should be in the format of servername/instancename. So I used this query to get both: select @@servername + '\' + @@servicename, and got "Myserver\MSSQLServer". However, using this as the $serverName argument caused the connection to fail. Only after I took out "\MSSQLServer" did it connect successfully. So as in the code above, the correct value to use was $serverName = "Myserver".

Cmaso
  • 897
  • 1
  • 7
  • 17