我有一个UBUNTU LAMP设置。安装了所有必要的扩展
php5-odbc
php5-mssql
FREETDS
unixODBC
从CLI我已经使用isql
和tsql
测试了我与数据库的连接,两者都正在连接并且工作正常。
这是我的freetds.conf
:
....
# A typical Microsoft server
[TS]
host = 10.32.6.52
port = 1433
tds version = 8.0
client charset = UTF-8
这是我的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
这是我的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
这是我的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);
?>
就像我从CLI说的那样我可以连接到SQL服务器,但是从php它不起作用。我已经尝试了所有可以找到的建议,但没有任何对我有用。
这里还有我的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
答案 0 :(得分:0)
找到问题并解决了。我通过取消注释/etc/freetds/freetds.conf
这是日志文件提取
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....
所以基本上它是在freetds.conf文件中查找我的servername。但我没有指明它。连接信息在配置文件中列为[TS]
我更改了我的php信息以查找[TS]
并且突然间它工作了
$myServer = "TS";
$username = "XXXXX";
$password = "XXXXX";
$database = "XXXXX";
答案 1 :(得分:0)
我只想发布此事件,以帮助其他可能遇到同样问题的人:
最终为我工作的是:将PHP
连接字符串中的freetds.conf
值更改为我尝试连接的服务器的引用名称,正如我在{{1}中指定的那样}和.odbc.ini
,而不是实际的IP
地址。
感谢@Stroes指出我正确的方向。经过一个多星期的阅读文章和故障排除后,这个问题几乎让我流下了眼泪。