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());
Related
I have tried to connect sybase from terminal using FreeTDS and it is working fine but I am not able to connect database using PHP.
I have done changes in below files.
/etc/odbc.ini file:
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Server =
Port =
Database =
Username =
Password =
freetds.conf file
[sybase]
host=
port=
Tds version=5.0
It is working fine using Terminal
tsql -S SYBASE -U username -P password
But from PHP connection I am getting error:
[unixODBC][Driver Manager]Data source name not found, and no default driver specified
PHP file code:
$db = ADONewConnection('odbc');
$DSN ='UID=username;PWD=password;EngineName=dbServiceName;AutoStop=No;Integrated=No;Debug=No;DisableMultiRowFetch=No;CommLinks=SharedMemory,TCPIP{};Compress=No;Driver={FreeTDS}';
$db->Connect($DSN );
I know in some versions, you need to use the IP of the server rather than the name, like this in freetds.conf:
[myserver]
host = 10.10.10.10
port = 5000
tds version = 5.0
I also don't know if tds version is case sensitive, but I've always seen it in lowercase. Are you sure that PHP is reading from the same freetds.conf that the command line version is?
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
I am trying to connect to a remote MSSQL server using the platforms listed in the title. I have FreeTDS and all the relevant ODBC packages installed. Here is my freetds.conf:
# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".
# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
; tds version = 4.2
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# 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
[DEVSQL]
host = x.x.x.x
instance = DEVSQL
tds version = 8.0
Here is my odbc.ini:
[ODBC Data Sources]
DEVSQL = FreeTDS Connection Server
[DEVSQL]
Description = MSSQL Server
Driver = freetds
ServerName = DEVSQL
Database = test
TDS_Version = 8.0
Here is my PHP code:
<?php
$dbname = "test";
$servername = "DEVSQL";
$username = "myuser";
$password = "mypassword";
try{
$db = new PDO('odbc:Driver=FreeTDS; Server='.$servername.'; Database='.$dbname.'; UID='.$username.'; PWD='.$password.';');
Database='.$dbname.'; UID='.$username.'; PWD='.$password.';');
}
catch(PDOException $exception){
die("Unable to open database.<br />Error message:<br /><br />$exception.");
}
echo '<h1>Successfully connected!</h1>';
?>
I am able to connect just fine using the following commands in the terminal:
TDSVER=8.0 tsql -S devsql -U myuser -P mypassword
as well as:
isql -v devsql myuser mypassword
But when I browse to index.php in FireFox I get the following error:
exception 'PDOException' with message 'SQLSTATE[08001]
SQLDriverConnect: 0 [unixODBC][FreeTDS][SQL Server]Unable to connect
to data source' in /var/www/html/index.php:16 Stack trace: #0
/var/www/html/index.php(16): PDO->__construct('odbc:Driver=Fre...') #1
{main}.
Any help much appreciated! Alos, if anyone has a better way to connect to sql server from a linux box using php, I'm all ears. Thanks again!
EDIT: I forgot to mention: Ubuntu and Apache are running inside a VM instance on a Win7 host. The Win7 environment on that same machine is where the SQL server is running. I'm not sure if that's relevant since they are communicating just fine, but thought I'd throw it out there.
I have to connect to a MSSQL server from Arch Linux with ODBC.
I use FreeTDS, and with isql, it's working:
isql sqlexpress dev Dev
But not in PHP.
I use PHP in interactive mode:
PHP > $conn = odbc_connect("sqlexpress", 'dev', 'Dev');
PHP > $a=odbc_exec($conn, 'SELECT * FROM measures;');
PHP Warning: odbc_exec(): SQL error: [FreeTDS][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 (""): Data type 0x00 is unknown., SQL state 37000 in SQLExecDirect in php shell code on line 1
Warning: odbc_exec(): SQL error: [FreeTDS][SQL Server]The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 1 (""): Data type 0x00 is unknown., SQL state 37000 in SQLExecDirect in php shell code on line 1
I've searched a lot, but I can't find any solution (or even somebody with the same problem).
My config files:
/etc/odbc.ini:
[sqlexpress]
Server = 192.168.10.39
Port = 1433
Driver = FreeTDS
Database = capture
UserName = dev
Password = Dev
/etc/odbcinst.ini:
[FreeTDS]
Description = FreeTDS driver
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so
Trace = Yes
TraceFile = /tmp/freetds.log
FileUsage = 1
UsageCount = 1
Have a nice day!
Mate
Here's my working configuration files for connecting to a MSSQL database from Ubuntu:
/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
[ebe]
Description = MSSQL Server
Driver = freetds
Database = my_database
ServerName = my_server_name
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
# Change the "no" to "yes" to enable ODBC logging.
[ODBC]
Trace = no
TraceFile = /tmp/odbc.log
/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.
[my_server_name]
host = my_server_domain_or_ip
port = 1433
tds version = 8.0
And finally, here's my PHP connection string:
$this->db_connection = new PDO("dblib:dbname=my_database;host=my_server_domain_or_ip", 'username', 'password');
I believe setting the tds version to 8.0 could be a big help to you.
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);