php can't connect to sql server 2008 (using pdo dblib) - php

I have a SQL server 2008 R2 (running on Windows server 2008 64bit), which I'm trying to connect from PHP
My PHP server configuration:
CentOS 6.6
PHP 5.5.24 (compiled --with-mssql=/usr/local/freetds --with-pdo-dblib=/usr/local/freetds)
Apache 2.4.12
SELinux is disabled (according to this solution: PDO DBLIB accessing SQL Server 2008 and 2012)
I wrote the following PHP code to connect to the SQL server:
try {
require "classes/mypdo.class.php";
$pdo = new MyPDO('dblib:dbname=myDB;host=myServer', 'myUser', 'myPassword');
$pdo->debug = true;
} catch (PDOException $e) {
die("Connection failed: {$e->getMessage()}");
}
The connection failed with an error:
Connection failed: SQLSTATE[HY000] Unknown host machine name (severity 2)
I tried other DSN syntaxes like:
$pdo = new MyPDO('dblib:host=192.168.0.10', 'myUser', 'myPassword');
$pdo = new MyPDO('dblib:host=192.168.0.10:1433', 'myUser', 'myPassword');
$pdo = new MyPDO('dblib:host=myServer', 'myUser', 'myPassword');
and many other variations...
When I use an IP address instead of DSN the error is:
Connection failed: SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)
/etc/freetds.conf
[global]
tds version = 7.1
dump file = /tmp/freetds.log
timeout = 10
connect timeout = 10
text size = 64512
client charset = UTF-8
[myServer]
host = 192.168.0.10
port = 1433
tds version = 7.1
I suspect that PHP ignoring freetds.conf (can't confirm it).
When I use tsql the connection is working.
tsql -S myServer -U MyUser -P MyPassword
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1>
What is the reason that PHP refusing to connect to the SQL server?

There are sometimes problems with an setting inside php.ini
Please try to change:
mssql.secure_connection = Off
to
mssql.secure_connection = On

Related

Could not find driver when connect PHP to SQL SERVER

I want to connect to SQL Server at another pc with php, when i try to connect it says
Connection error: could not find driver
What I have tried is add extension in php.ini.
;extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
my PHP version is: 7.2.3
SQL SERVER: SQL Server 2014
OS PC: Windows Server 2012 R2 (another pc)
here's my code:
<?php
$host = "192.168.3.126";
$db_name = "dbo";
$username = "david";
$password = "david";
try {
//$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$db = new PDO("sqlsrv:Server={$host};Database={$db_name}", $username, $password);
}
catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage();}
?>
NB: I have download Microsoft Drivers 5.6 for PHP for SQL Server at here, I have download install the ODBC Driver 17 at my own pc
I have tried this reference but it doesn't work PHP Sql Server PDOException:could not find driver

SQLSTATE[01002] Adaptive Server connection failed (severity 9) error on Ubuntu Linux VPS

I am trying to connect to an Azure Microsoft SQL Server database on my php scripts. I cannot figure out why it isn't working. When I run my db_connection.php script, I get this error:
SQLSTATE[01002] Adaptive Server connection failed (severity 9)
When I run the tsql command, with the connection details for my azure ms sql database, the connection seems to work (I read the "1>" means the connection worked):
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "UTF-8"
Default database being set to iBalekaDB
1>
Inside my freetds.conf file, I have this configuration set up:
# server specific section
[global]
# TDS protocol version
tds version = 8.0
text size = 20971520
client charset = UTF-8
dump file = /tmp/freetds.log
debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
[iBalekaServer]
host = xxxxxxxx.xxxxxxx.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
My db_connection.php file looks like this:
try {
$dataSource = "dblib:host=iBalekaServer;dbname=iBalekaDB;";
$username = "xxxxxxxxxxxx";
$password = "xxxxxxxxxxxx";
$connectionObject = new PDO($dataSource, $username, $password);
$connectionObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($connectionObject) {
echo "<h2>Connection Successful</h2>";
} else {
echo "Connection Error";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
I ran tsql -C on the VPS and got this:
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
I checked to see if I had pdo_dblib installed, and it was present when I ran phpinfo() on my Linux VPS Server.
What could be the issue here?
EDIT: using mssql_connect works. I really wanted to use PDO
On my test, I changed the $username to the format of UID (e.g. <username>#<db_server_name>), and it fixed your issue of SQLSTATE[01002] Adaptive Server connection failed (severity 9).
BTW, you can grab the UID from the connectionstring from Azure portal.
Additionally, if you get the issue of General SQL Server error: Check messages from the SQL Server (severity 16), you can refer to the answer of PDO DBLib not working.
Any update, please feel free to let me know.

Cannot connect to MS SQL Server using PDO

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
);

Apache will not connect to SQL Server with PHP

I'm trying to open a connection to a SQL Server 2008 on a windows computer from an apache linux server via php. I've opened up the appropriate port on the firewall, but I am getting the vaguest error
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: xxx.xxx.xx.xxx,xxxx
with:
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
$myUser = "un";
$myPass = "pw";
$myDB = "db";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());
Is there some way to get a more specific error message? Or some way that I can test to see if the two computers are communicating at all so I can try to begin to localize the problem?
Here's the code I use to connect PHP to MSSQL from Ubuntu machines to Windows SQL Server, I don't know if it will help you or not but this code is up and running successfully right now so I know it works in our environment...
As you can see, I use PDO rather than the mssql_* functions. On Ubuntu I needed to install the php5-sybase package to get the dblib driver.
PHP:
<?php
try{
$con = new PDO("dblib:dbname=$dbname;host=$servername", $username, $password);
}catch(PDOException $e){
echo 'Failed to connect to database: ' . $e->getMessage() . "\n";
exit;
}
?>
/etc/odbc.ini
# Define a connection to the MSSQL server.
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssqldb]
Description = MSSQL Server
Driver = freetds
Database = MyDB
ServerName = mssqldb
TDS_Version = 8.0
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf
[global]
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512
# Define a connection to the MSSQL server.
[mssqldb]
host = mssqldb
port = 1433
tds version = 8.0
I ran into the same problem today. This works for me:
Instead of this
$myServer = "xxx.xxx.xx.xxx,1433"; //with port number; tried both backslash and colon
try this:
$myServer = "mssqldb"; //must correspond to [entry] in freetds.conf file
In your case, I'd rename the entry and use the fully qualified hostname to the entry in the config file just for clarity
# Define a connection to the MSSQL server.
[mssqldbAlias]
host = mssqldb.mydomain.com
port = 1433
tds version = 8.0
The first argument in the call to mssql_connect() must correspond to an [entry] in the freetds.conf file.
So your call becomes
$myServer = "mssqldbAlias";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die( mssql_get_last_message());

Using a FreeTDS ODBC driver to connect Linux PHP to a SQL Server

I just set up my freetds.conf file with a new virtual account to connect to a SQL Server database but I am not sure when and where I tell my PHP script which database to connect to below are my settings
odbc.ini
[McDo]
ServerName = server1
Driver = FreeTDS
Description = MyServer
Trace = Yes
freetds.conf
[server1]
host = 66.111.xxx.xxx
port = 1433
tds version = 7.0
And here is my PHP connect script. Now sure where I tell the script to connect to which database.
putenv('ODBCINI=/etc/odbc.ini');
$connect = odbc_connect("server1", "username", "password");
//$query = "SELECT name_ID FROM ext_name";
if(!$connect){
echo "not connected";
}else{
echo "connected";
}
odbc_close($connect);
The php manual shows:
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

Categories