4

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.

  • Does the database test exist? Does "myuser" have rights to the test database? Can you connect to a different ODBC source? Can you post the SQL Server errorlog relevant messages (if any)? – leancz Jul 25 '14 at 12:21
  • Yes the database exists and the credentials are valid. As I mentioned above, I am able to connect successfully via isql and tsql. I even ran simple queries in isql and was able to access tables in the test db. I don't have any other ODBC sources set up. SQL server is not throwing any errors.One thing 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 (edited above). – CanadaGoose Jul 25 '14 at 13:44
  • Still banging my head against the wall with this. Any ideas? Anyone? – CanadaGoose Aug 11 '14 at 00:00
  • What does the /etc/freetds/freetds.conf look like? Is the MSSQL host listed? Check out the answer on this question and see: [link](http://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc) – tatorface Oct 10 '14 at 22:18

0 Answers0