php dblib, Error: SQLSTATE[HY000] Unknown host machine name (severity 2)

隐身守侯 提交于 2019-11-30 04:37:17

问题


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

回答1:


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.




回答2:


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');
    }
?>  


来源:https://stackoverflow.com/questions/29570235/php-dblib-error-sqlstatehy000-unknown-host-machine-name-severity-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!