PHP connection to SQL Server 2008 - php

I need to connect from PHP to an SQL Server 2008 instance. The Webserver is Apache 2.4 on a Centos 8 with PHP 7.4, php-sqlsrv extension, and I am using PDO to handle the connection.
First I tested the connection from cli, and I got the following error:
Connection failed: SQLSTATE[08001]: [Microsoft][ODBC Driver 17 for SQL Server]SSL Provider: [error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol]
A little bit of googling sent me to a direction toward openssl.cnf, where I set the minimum version to TLS 1.0 (I know it's not secure, but it seemed to be working), and then connection worked, but only in cli, when I try it from the site, it still gives the same error.
The code I use for the connection is this (I changed the connection data):
try {
$conn = new PDO("sqlsrv:Server=database.local,1433;Database=database", "user", "password");
}
catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Can you help me figure out what I missed?
Thank you

update-crypto-policies --set LEGACY

Related

PDO from hosting to SQL Server

I have a hosting with cPanel and a Windows VPS with SQL Server. I'm trying to do a PDO connection to SQL Server but I keep getting this error:
The Following error has occurred: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 x64: https://go.microsoft.com/fwlink/?LinkId=163712
I am a little bit confused am I supposed to install driver in Windows VPS or it is a hosting issue?
This is my simple PHP code:
try {
$con = new PDO('sqlsrv:Server=IPADDRESS;Database=DBNAME', 'sa', 'PASSWORD');
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "The Following error has occurred:".$e->getMessage()."";
Btw sqlsrv, pdo_sqlsrv and odbc are enabled in Hosting.
Help please
You must install the driver in Windows, so follow the instructions on https://learn.microsoft.com/en-us/iis/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php

PHP Sql Server PDOException:could not find driver

My server is a Windows 2008 server. PHP Version 7.2.7 is installed and running. Sql Server 11 (64 bit) is installed and is working (there is a couple asp.net apps running and already using that database)
I downloaded the PHP Sql Server Drivers from Microsofts website and placed the .dll files in the PHP ext directory.
In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64
In my .php file I am using to test my db connection I have:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
I am getting:
PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7 (Line 7 is the $SqlServerCon line).
What did I do wrong? and What do I need to do to get a connection to Sql Server?
I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.
For future reference for anyone else with this or a similar issue...
It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.
In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
More info can be found at https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017
I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.
PDO connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword");
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}
sqlsrv_connect connection test:
$SqlServer = "THISSERVER\SQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
As commentators said - adding .dll at the end of extension= config line should be good start with your issue.
I can also see that you're trying to load NTS version of extension (NTS stands for non thread safe). Are you sure that you're going to load right version of extension? Please check if you're running PHP as NTS runtime or not - you can check it with phpinfo();.

PHP timeout when connecting to mssql under apache

PHP Version 5.4,
Apache Version 2.4,
CentOS 7.3
Trying to connect to mssql db using the following php code which works fine when I run it from the command line. However, when I place code under apache (on same server) and call through browser I get a timeout error.
Error: Connection failed: SQLSTATE[HYT00] SQLConnect: 0
[unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout
expired+
set_time_limit(30);
echo "+ Connection\n";
try {
$pdo = new PDO("odbc:sqlsrv_msodbc", "username", "password");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
I just had the same issue. I am using CentOS 7.3 in vagrant and solved it by disabling SELinux.
If you're on a dev environment and disabling SELinux is cool you can do it by:
sudo vi /etc/sysconfig/selinux
Set
SELINUX=disabled
I found this over at serverfault: https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux

PHP v5.6 PDO on MS SQL Server 2014

Issue
I am currently unable to connect to my database using PDO in PHP.
Specs
PHP version 5.6.9
Database: Microsoft SQL Server 2014 on 2008 R2
From phpinfo(): PDO drivers: mysql, odbc, sqlite
Using IIS 6.1
What I've tried
I have just installed the drivers in the SQLSRV32.exe file from Microsoft (from this link: https://www.microsoft.com/en-us/download/details.aspx?id=20098) to my PHP extensions folder, however I still cannot seem to connect to my database. (And yes, I restarted IIS)
I can get the connection to work with older connection methods.
My Code
My current PHP code for PDO connection is as follows:
try {
$this->pdo = new PDO('odbc:host='.$this->hostname.':'.$this->port.';dbname='.$this->database,$this->username,$this->password);
} catch (PDOException $e) {
echo 'Failed to get DB handle: '.$e->getMessage().'<br/>';
exit;
}
The above code is inside my MyPDO class constructor so that when I instantiate my class, it is connected $pdo = new MyPDO();.
My Code's Result
The issue is that I am getting this error as output:
Failed to get DB handle: SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Any help resolving this issue is greatly appreciated!
Double check your php.ini and confirm that you have lines:
extension=php_sqlsrv_56_nts.dll
extension=php_pdo_sqlsrv_56_nts.dll
For PDO only php_pdo_sqlsrv_56_nts.dll is mandatory.
In phpinfo() in PDO Drivers sections you should have: sqlsrv
Use PDO_SQLSRV DSN rather than odbc, for example:
$this->pdo = new PDO( "sqlsrv:server=$hostname,$port;Database=$dbname", $username, $password);
or if you have full datasource name (ex. localhost\SQLEXPRESS)
$this->pdo = new PDO( "sqlsrv:server=$serverName;Database=$dbname", $username, $password);
If you receive could not find driver exception it means that sqlsrv extensions are not loaded.

pdo_odbc driver issue

I try to running PDO with an ODBC connection.
First at all im running on a 64 bits windows with a 32 bits WAMP installation and finally a 32 bits ODBC driver (does not exist in 64 bits).
Thanks to :
C:\Windows\SysWOW64\odbcad32.exe
I have created a DSN test and I tried odbc_connection :
odbc_connect("test", "root" , "root");
Connection works and i can perform query.
Then I tried pdo_odbc :
try {
$dbh = new PDO("odbc:test", "root", "root");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
but I get an error ...
Connection failed: could not find driver
I can't understand why pdo connection failed :'(
PS: pdo_odbc is enabled on phpinfo(); see below
I think your DSN connection string is incorrect. I think it should be something like;
odbc:Driver={Driver Name}...
Take a look at some of the examples on the PHP site. It may give you some clues as to what your DSN connection string should be...
http://php.net/manual/en/ref.pdo-odbc.connection.php
Finaly it's just more stupid... The ODBC driver (nuoDB win ODBC driver) I used was not designed for working with PDO.

Categories