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.
Related
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
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.
The last 2 days I've spent a lot of time trying to figure out how to connect to MS SQL server hosted on Azure using PHP from an Ubuntu 14.04 server.
The code responsible to initiate the connection to the remote database is the following:
public function connect()
{
try
{
$this->databaseConnection = new \PDO(
'dblib:host='.$this->hostname.':'.$this->port.';dbname='.$this->databaseName.';',
$this->username,
$this->password
);
$this->databaseConnection->setAttribute(
\PDO::ATTR_ERRMODE,
\PDO::ERRMODE_EXCEPTION
);
}
catch (\PDOException $e)
{
throw new \Exception('Failed to connect to the database: '.$e->getMessage());
}
}
And every time I try to connect I receive:
Failed to connect to the database: SQLSTATE[01002] Adaptive Server connection failed (severity 9)
The problem is that this is a very general error and there's no way for me to understand what's causing the issue.
This is my /etc/freetds.conf configuration:
[placeholder.database.windows.net]
host = placeholder.database.windows.net
port = 1433
tds version = 7.2
client charset = UTF-8
And this is the tsql -C output:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
In addition to this, I tried making some test using mssql_* functions and even if I was able to successfully connect to the database most of the queries didn't work.
For a simple solution for one sqlsrv server connection in your application, you can modify the tds version to 8.0 under the [global] section of the freetds.conf, e.g.
[global]
tds version = 8.0
Then test in PHP script:
try {
$pdo = new PDO("dblib:host=<azure_sql_name>.database.windows.net;dbname=<dbname>", "<username>", "<password>");
} catch (PDOException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
It works fine on my side after the modification. Any further concern, please feel free to let me know.
The problem was caused by 2 different issues.
The first one was related to a wrong FreeTDS configuration. The freetds.con global section needs to be like this:
[global]
tds version = 8.0
The second one was caused by the fact that the username must be specified in the following way when connecting to a MSSQL server hosted on Azure:
<username>#<freeTDSServerName>
Where the specific server configuration in freetds.conf looks something like this:
[<freeTDSServerName>]
database = <databaseName>
host = <serverName>.database.windows.net
port = 1433
tds version = 8.0
So the PHP DB connection will look like something like this:
$this->databaseConnection = new \PDO(
'dblib:host='.$this->hostname.':'.$this->port.';dbname='.$this->databaseName.';',
$this->username.'#<freeTDSName>',
$this->password
);
Basically the title,
When I attempt to connect to a database I get the error: could not find driver.
I've searched online and it was said I had to uncomment several extensions.
I uncommented extension=php_pgsql.dll and extension=php_pdo_pgsql.dll in the php.ini in C:\xampp\php, but that didnt solve it.
When I run my code on the online server the database connection works.
I'm using PDO for the database to a MSSQL server.
Basic information:
I'm using XAMPP Control Panel V3.2.2 on Windows
I connect like so:
define("USER_NAME", "myusername");
define("DATABASE", "mydatabase");
define("PASSWORD", "mypassword");
define("HOST", "myhost");
try{
$db = new PDO("dblib:host=".HOST.";dbname=".DATABASE, USER_NAME, PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){
echo $exception->getMessage();
exit;
}
EDIT
I downloaded MSSQL driver for PDO.
I added these two extensions:
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
Without success.
-EDIT-
You enabled the wrong extension to use MSSQL database.
So you could check the dblib docs for the dll (tip from https://stackoverflow.com/users/934966/jon-stirling).
-ORIGINAL-
You forgot the database server port by the host (host=".HOST.":".PORT.";):
define("USER_NAME", "myusername");
define("DATABASE", "mydatabase");
define("PASSWORD", "mypassword");
define("HOST", "myhost");
define("PORT", "10060");
try{
$db = new PDO("dblib:host=".HOST.":".PORT.";dbname=".DATABASE, USER_NAME, PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $exception){
echo $exception->getMessage();
exit;
}
Hope this works!
More information:
http://php.net/manual/en/ref.pdo-dblib.php
You can't use PDO_dblib in XAMPP3.2.2
This extension is not available anymore on Windows with PHP 5.3 or later.
On Windows, you should use SqlSrv
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