1
votes
  • Windows Server 2016 64 bit (Fully Updated)
  • WAMP/3.1.3 (Win32)
  • Apache/2.4.33 (Win32)
  • PHP/7.2.4 (Win32)

I am trying to connect to my database on a SQL Server but can't seem to get the connection working. I have tried several approaches, downloaded files, enabled extensions but none of them seem to work. I have established that the SQL Server has Named Pipes and that Remote Connections are allowed. I have also confirmed that he password and username work:

<?php

$driver='{ODBC Driver 17 for SQL Server}';
/* $driver='{ODBC Driver 13 for SQL Server}'; */
/* $driver='{ODBC Driver 11 for SQL Server}'; */
$dsn='phpodbc11';
$hostname='host01';
$database='data01';
$username='root';
$password='pass123';

$conn = new PDO("odbc:$dns")
$conn = new PDO("dblib:host=$hostname;dbname=$database", $username,$password);
$conn = new PDO("sqlsrv:Server=$hostname;Database=$database", $username, $password);
$conn = new PDO("odbc:Driver=$driver;Server=$hostname;Database=$database", $username, $password);

?>

Drivers Loaded on the Server System DSNs loaded

php.ini extensions :

  • extension=pdo_odbc
  • extension=php_mysqli.dll
  • extension=php_ldap.dll
  • extension=php_pdo_sqlsrv_72_ts_x86.dll
  • extension=php_sqlsrv_72_ts_x86.dll

Errors I get for each of the aforementioned new PDO attempts

*SQLSTATE[IM002] SQLConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

could not find driver

SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x86: https://go.microsoft.com/fwlink/?LinkId=163712

SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified*

1

1 Answers

0
votes

Try this, it works for me:
1.- Execute PDO Drivers/Version 3_2/SQLSRV32.exe
Download from PDO Drivers

First see compatibillity in [Drivers for PHP for SQL Server][2]

2.- Copy this DLLs in your WAMP directory

  • php_sqlsrv_56_ts.dll
  • php_pdo_sqlsrv_56_ts.dll
  • php_pdo_sqlsrv_56_nts.dll

3.- Add this extension lines in your php.ini section Dynamic Extensions:

extension=php_sqlsrv_56_ts.dll  
extension=php_pdo_sqlsrv_56_ts.dll  
extension=php_pdo_sqlsrv_56_nts.dll  

4.- Restart apache WampServer
5.- Installl ODBC 11 in your machine:
ODBC

Run: ODBC/msodbcsql.msi  

6.- See phpinfo.php, now you must be this:

PDO  
PDO support enabled  
PDO drivers mysql, sqlite, **sqlsrv**  

pdo_sqlsrv  
pdo_sqlsrv support  enabled  
Directive   Local Value Master Value  
pdo_sqlsrv.client_buffer_max_kb_size    10240   10240  
pdo_sqlsrv.log_severity 0   0  

sqlsrv  
sqlsrv support  enabled  
Directive   Local Value Master Value  
sqlsrv.ClientBufferMaxKBSize    10240   10240  
sqlsrv.LogSeverity  0   0  
sqlsrv.LogSubsystems    0   0  
sqlsrv.WarningsReturnAsErrors   On  On  

Good luck