简体   繁体   English

FreeTDS mssql_connect无法正常工作

[英]FreeTDS mssql_connect not working

I have a UBUNTU LAMP setup. 我有一个UBUNTU LAMP设置。 All the necessary extensions installed 已安装所有必需的扩展

php5-odbc php5-mssql FREETDS unixODBC php5-odbc php5-mssql FREETDS unixODBC

From CLI i have tested my connection to the DB using isql and tsql and both are connecting and working perfectly. 从CLI中,我已经使用isqltsql测试了我与数据库的连接,并且两者都可以正常连接和正常工作。

Here is my freetds.conf : 这是我的freetds.conf

    ....
    # A typical Microsoft server
[TS]
        host = 10.32.6.52
        port = 1433
        tds version = 8.0
        client charset = UTF-8

Here is my odbc.ini 这是我的odbc.ini

[TS]
Description = "XXXXXXX"
Driver = /usr/local/lib/libtdsodbc.so
Server = xxx.xxx.xxx.xxx
Port = 1433
Database = XX_X_XX_XXXXX
Trace = No
TDS_Version = 8.0
[Default]
Driver=/usr/local/lib/libtdsodbc.so

Here is my odbcinst.ini 这是我的odbcinst.ini

[FreeTDS]
Description = FreeTDS
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
FileUsage = 1
CPTimeout =
CPResuse  =
client charset = utf-8
UsageCount = 1

And here is my php 这是我的PHP

<?php

putenv("TDSVER=80");
putenv("FREETDSCONF=/etc/freetds/freetds.conf");
putenv("ODBCSYSINI=/etc/odbcinst.ini");
putenv("ODBCINI=/etc/odbc.ini");


$myServer = "XXX.XXX.XXX.XXX";
$username = "XXXXX";
$password = "XXXXX";
$database = "XXXXX";

//connection to the database
$dbhandle = mssql_connect($myServer, $username, $password)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($database, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT title from project where id = 8 ";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["title"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

Like i said from CLI i can connect to the SQL server, but from php it doesnt work. 就像我从CLI所说的那样,我可以连接到SQL Server,但是从php无法正常工作。 I have tried all the suggestions i could find but nothing is working for me. 我已经尝试了所有可以找到的建议,但没有任何帮助。

Also for reference here is my Apache Error Log: 我的Apache错误日志也供您参考:

[Tue Jun 09 08:32:41.931108 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP Warning:  mssql_connect(): Unable to connect to server: XXXXXXXX in /var/www/html/test/sqltest.php on line 15
[Tue Jun 09 08:32:41.931124 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP Stack trace:
[Tue Jun 09 08:32:41.931134 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP   1. {main}() /var/www/html/test/sqltest.php:0
[Tue Jun 09 08:32:41.931142 2015] [:error] [pid 21435] [client 10.32.6.65:54994] PHP   2. mssql_connect() /var/www/html/test/sqltest.php:15

Found the issue and resolved it. 找到问题并解决。 I enabled the Freetds.log by uncommenting the log file location in /etc/freetds/freetds.conf 我通过取消注释/etc/freetds/freetds.conf的日志文件位置来启用Freetds.log。

Here is the log file extract 这是日志文件摘录

config.c:303:[XXXXXXXXX] not found.
config.c:367:... $FREETDS not set.  Trying $HOME.
config.c:293:Could not open '/var/www/.freetds.conf' ((.freetds.conf)).
config.c:297:Found conf file '/usr/local/etc/freetds.conf' (default).
config.c:483:Looking for section global.
config.c:542:   Found section global.
config.c:545:Got a match.
config.c:567:   text size = '64512'
config.c:542:   Found section egserver50.
config.c:542:   Found section egserver70.
config.c:556:   Reached EOF
config.c:483:Looking for section XXXXXXXXXXXX.
config.c:542:   Found section global.
config.c:542:   Found section egserver50.
config.c:542:   Found section egserver70.
config.c:556:   Reached EOF
config.c:303:[XXXXXXXXXXXX] not found.
config.c:208:Failed in reading conf file.  Trying interface files.
config.c:1042:Looking for server XXXXXXXXXXX....

So basically it was looking for my servername in the freetds.conf files. 因此,基本上它是在freetds.conf文件中寻找我的服务器名称。 but i didnt specify it. 但我没有指定。 the connection information was listed in the config file as [TS] i changed my php information to look for [TS] and the all of a sudden it worked 连接信息在配置文件中列为[TS]我更改了php信息以查找[TS] ,突然之间一切正常

$myServer = "TS";
$username = "XXXXX";
$password = "XXXXX";
$database = "XXXXX";

I just wanted to post this in the event it helps anyone else who may be experiencing the same issue: 我只是想将其发布,以帮助其他可能遇到相同问题的人:

What ended up working for me: Changing the $host value in my PHP connection string to the reference name of the server I was trying to connect to,as I specified it in freetds.conf and .odbc.ini , instead of the actual IP address. 最终对我freetds.conf是:将我的PHP连接字符串中的$host值更改为我尝试连接的服务器的引用名称,就像我在freetds.conf.odbc.ini指定的freetds.conf ,而不是实际的IP地址。

Thank you @Stroes for pointing me in the right direction. 感谢@Stroes为我指出正确的方向。 This issue literally almost brought me to tears after over a week of reading articles and troubleshooting. 经过一个多星期的阅读文章和故障排除,这个问题几乎使我流泪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM