7

I am using mac computer OSX 10.9. Freetds and unixODBC are already installed on my computer and added as extension to php , trying to connect to a remote MSSQL server. Below is my connection testing:

<?php 
$dbh = new PDO('dblib:host=Hostname ;dbname=Dbname', 'user', 'pw'); 
if (!$dbh) {
    die('Something went wrong while connecting to MSSQL');
}
?>

The error logs file show :

[error] [client 127.0.0.1] PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Unknown host machine name (severity 2)

What could be the problem ? It seems that my freetds and unixODBC are working fine if I use terminal to connect to the same database as below:

$ isql Hostname user pw 
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> 

and

$ tsql -S Hostname  -U user
Password: 
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
1> 

here is my freetds.conf

[global]
    # TDS protocol version
    tds version = 8.0 
[Hostname]
host = IP
port = 1433
tds version = 8.0
client charset = UTF-8 ##needed on MAC OS X
dump file = /tmp/freetds.log

and my odbc.ini

[Hostname]
#Driver=/usr/local/lib/libtdsodbc.so
Driver = /usr/local/Cellar/freetds/0.91_2/lib/libtdsodbc.so
Trace=No
Server=IP
Port=1433
TDS_Version=8.0
client charset = UTF-8  

my phpinfo() shows that the extension has been added, there is dblib in PDO section and pdo_dblib section have driver Flavour enabled freetds.

So what is the problem? Any idea of what I should do ? Any assistance will be highly appreciated.

here is my odbcinst.ini:

[freetdS]
Description = v0.63 with protocol v8.0 
Driver = /usr/local/Cellar/freetds/0.91_2/lib/libtdsodbc.so
na83
  • 111
  • 2
  • 8
  • Did you restart Apache (or whatever web server you're using) after you changed these configuration files? – Benny Hill Apr 11 '15 at 00:18
  • Also, I notice in your odbc.ini that `Hostname` is misspelled (at least the code you show here) - you show it as `Hostnmae`. – Benny Hill Apr 11 '15 at 00:21
  • [My answer](http://stackoverflow.com/questions/20163776/connect-php-to-mssql-via-pdo-odbc/20165384#20165384) to a similar question may be helpful. Take a look at the `odbcinst.ini` configuration and in my `odbc.ini` file I have a `ServerName` parameter but not a `Server` parameter. – Benny Hill Apr 11 '15 at 00:23
  • Yes, I tried restart apache and nothing change, and the Hostnmae is just my typing error, they are same in my files. I added my odbcinst.ini – na83 Apr 11 '15 at 16:26
  • If I change my Server to ServerName ,tsql amd isql won't work – na83 Apr 11 '15 at 16:28
  • The only thing that sticks out to me is you don't have a `Setup` or `UsageCount` parameter and I do. Sorry I'm not of more help. – Benny Hill Apr 11 '15 at 20:08
  • Unfortunately no one is going to be able to solve this without the real and actual connection ini file info. There might be a syntax or information typo in the files. Also hostnames can be a fully qualified domain name or an ip. You might be protecting your server info by not posting it here but you might also be protecting the error. – Carl McDade Apr 14 '15 at 12:09
  • In my case, it is always working fine, but suddenly i'm randomly getting this error. What I did to solve this is I restarted the webserver. I'm assuming there is some cache or logs that has size limit, and restart the webserver would clear that up. – sulaiman sudirman May 23 '18 at 03:43

2 Answers2

2

I actually solved this question by deleting both mssql.so, pdo_dblib.so in php extension folder, re-download php5.4 , phpize and build both the .so files again and put it back. Then it works.

It seems that the olde pdo_dblib.so file I made pointed to a different freetds.conf somewhere else.

na83
  • 111
  • 2
  • 8
  • `locate freetds.conf` will give you a clue if there are multiple conf files hanging around (although you may have to first set up the db for that on OSX). – Kristen Waite Feb 03 '17 at 16:35
0

Since it is not possible to debug this by proxy without real files information and you've opted to protect ip addresses and hostnames with dummy text. I am going with the PHP code. The hostname should be a FQDN or an ip address. Not the place holder text from the ini files.

<?php 
    $dbh = new PDO('dblib:host=[ ip address || example.com || localhost] ;dbname=Dbname', 'user', 'pw'); 
    if (!$dbh)
    {
        die('Something went wrong while connecting to MSSQL');
    }
?>  
Carl McDade
  • 634
  • 9
  • 14