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.
Related
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
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
I am trying to connect to a MS SQL server with PHP in a Docker environment.
I followed this instructions for my Dockerfile:
FROM php:7.3.10-apache
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
The following two lines are added to the php.ini:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
The phpinfo() shows this:
Now I am trying to connect to the MS SQL Server (which is not part of the docker environment):
$server = 'MyServer';
$database = 'MyDB';
$username = 'MyUser';
$password = 'MyPassword';
# Connect
try {
$conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error connecting to SQL Server: ".$e->getMessage());
}
But I am getting the following error:
Error connecting to SQL Server: 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
Can someone point me in the right direction? What exactly do I need to do?
So I'm using Linux (ubuntu) and I'm trying to connect to a Microsoft Azure SQL database.
Server information:
The server address is a1a1a1a1a1.database.windows.net (a1a1a1a1a1 is a placeholder for my server name)
The database name is MyDatabase
The table is called [dbo].[Sloth]
Freetds.conf settings:
[global]
dump file = /tmp/freetds.log
debug flags = 0xffff
text size = 64512
[a1a1a1a1a1.database.windows.net]
host = a1a1a1a1a1.database.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
When I run
php -r "phpinfo();" | grep "PDO drivers"
in terminal it returns
PDO drivers => dblib, mysql
so to the best of my knowledge all the configuration and driver installations are how they should be.
So now for the errors:
ERROR 1
If I initialize the PDO like so:
$conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
it connects, but if I remove the spaces around the '=' character for dbname like so:
$conn = new \PDO ( "dblib:dbname=MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
it returns these errors from the PDOException and freetds.log
SQLSTATE[HY000] General SQL Server error: Check messages from the SQL Server (severity 16)
(dbutil.c:86):msgno 40508: "USE statement is not supported to switch between databases. Use a new connection to connect to a different Database."
ERROR 2
When I run the following code
try {
$conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
$conn->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
$statement = $conn->prepare("SELECT * FROM dbo.Sloth");
$result = $statement->execute();
}
catch ( PDOException $e ) {
print( "Error connecting to SQL Server." );
die(print_r($e));
}
it breaks at the $result = ... line with these errors from the PDOException and freetds.log
SQLSTATE[HY000]: General error: 208 General SQL Server error: Check messages from the SQL Server [208] (severity 16) [(null)]
(dbutil.c:86):msgno 208: "Invalid object name 'dbo.Sloth'."
but when I run the query
SELECT * FROM dbo.Sloth
in the Azure Database Management Portal it returns all the correct rows.
Any help with these issues will be much appreciated!
EDIT
Through some tests I've discovered that both problems stem from the fact that the PDO isn't connecting to the database but is instead connecting to the "master". This would explain ERROR 2's Invalid Object Name error.
Any thoughts as to why the code in ERROR 1 doesn't connect to MyDatabase?
SOLUTION
I was using PHP 5.3.10 which had the following bug still in it, https://bugs.php.net/bug.php?id=64338
Turns out the PDO driver was making a USE statement which Microsoft Azure SQL Database doesn't like.
I upgraded to PHP 5.4.21 and everything worked properly.
I'm running php5 on Ubuntu10.10 server where the unixODBC and php5-odbc packages have already been installed. I keep getting an error during the connection process - code:
$data_source = 'DRIVER={Microsoft dBASE Driver (*.dbf)};Data Source=//128.251.xxx.xxx/lv_apps/AppsLON/CData/dbf/cdma';"", "");
$conn = odbc_connect($data_source, "", "");
if (!$conn)
exit("Connection Failed: " .$conn );
error message:
1 Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect in /var/www/vfptest.php on line 6
I added extention=dbase.so and extention=odbc.so to both files: /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini and re-compiled the php install with:
sudo ./configure --prefix=/usr --enable-dbase=shared --with-unixODBC
I've also gone to unixODBC and pecl to install the unixODBC and dbase libraries separately with no luck.
Also I found this link: http://www.devlist.com/ConnectionStringsPage.aspx
where it is stated that in order to create an ODBC connection to a foxpro database I use the line:
Driver={Microsoft Visual FoxPro Driver};SourceType=DBC;SourceDB=c:\demo.dbc;Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO
this still did not work for me however; probably because I do not have the visual foxpro driver installed or my odbc.ini file pointing to the driver path...but I don't know.
In short - odbc_connect() takes; an ODBC Data Source Name (DSN), Username & Password as per --
http://php.net/manual/en/function.odbc-connect.php
As far as I am aware there is no VFP ODBC driver for Linux so you will, most likely, need to employ an ODBC Bridge solution like -
http://uda.openlinksw.com/odbc-odbc-mt/
This has a client/server architecture --
Linux client --
php ODBC Application
OpenLink Generic ODBC Driver (thin multi-tier ODBC client)
Windows server --
OpenLink Request Broker (Multi-tier server)
OpenLInk Agent for ODBC (Multi-tier server)
Fox Pro ODBC DSN (pre configured)
Fox Pro files...
I hope this helps?
Check out the very bottom of this thread on the MSDN network
the driver line you have posted is "...an ODBC connection string which is not supported for VFP tables having a version later than 6."
Although not familiar with linux connecting to VFP, however, odbc connections for such VFP connect to a PATH, not a specific table. Once the connection to the PATH is valid, THEN you can query any .DBF table IN that location.
Although I can't answer your specific connection info, I've found this link
http://www.phpfreaks.com/forums/index.php?topic=296832.0
that shows connecting to a datasource... notice its identifying the 'DRIVER={Microsoft dBASE Driver (*.dbf)};datasource=/home/dir/file.dbf;' used... You'll probably have to hit a combination of this driver and pathing info (I'd still try WITHOUT the specific .dbf file first).
Hope this helps..