PDO from hosting to SQL Server - php

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

Related

PHP connection to SQL Server 2008

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

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 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.

Connect PHP to AS400 with ODBC or DB2

I'm trying to connect my WEB server to AS400. The web server has not DB2 library neither ODBC library.
I have installed XAMPP in my Windows computer, and one of my colleagues also did.
He has Client Access on his Computer.
We both installed:
- XAMPP
- ibm_data_server_driver_package_win32_v10.5.exe
We tried to install PECL DB2 extension (LINK) but unsuccessfully (got error ".\php.exe appears to have a suffix .exe, but config variable php": seems that nobody has solved this problem on Windows...).
Then we saw that XAMPP has the ODBC Module already on it, so we tried to estabilish a connection with obdc_connect. Referring to THIS question we are now able to connect to AS400 using his computer with Client Access Drivers using:
$user = 'USER';
$password = 'PASS';
$hostname = '192.168.1.30';
$server="Driver={Client Access ODBC Driver (32-bit)};
System=$hostname;
Uid=$user;
Pwd=$password;";
odbc_connect($server, $user, $password);
With my computer I tried to use the IBM data server driver already installed using:
$user = 'USER';
$password = 'PASS';
$hostname = '192.168.1.30';
$server="Driver={IBM DB2 ODBC DRIVER};
System=$hostname;
Uid=$user;
Pwd=$password;";
odbc_connect($server, $user, $password);
and I get always this error:
Warning: odbc_connect(): in C:\xampp\htdocs\test.php on line 11
When I've tried with IBM DB2 ODBC DRIVER on my colleague's computer, I've also got the same error.
What is this error? No information is specified.
We would like to use db2_connect instead of odbc_connect. What should we do to install this extension? I asked my ISP to install the db2 extension on the WEB server but I'm still waiting... maybe he also encountered some problems (the WEB server is a UNIX machine).
Any help is much apprecciated!

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Categories