gpt4 book ai didi

php - FreeTDS mssql_connect 不工作

转载 作者:行者123 更新时间:2023-12-04 18:51:52 28 4
gpt4 key购买 nike

我有一个 UBUNTU LAMP 设置。安装了所有必要的扩展
php5-odbcphp5-mssqlFREETDSunixODBC
在 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

最佳答案

发现问题并解决。我通过取消注释 /etc/freetds/freetds.conf 中的日志文件位置来启用 Freetds.log

这是日志文件提取

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 文件中寻找我的服务器名。但我没有具体说明。连接信息在配置文件中列为 [TS]我更改了我的 php 信息以查找 [TS]突然之间它起作用了
$myServer = "TS";
$username = "XXXXX";
$password = "XXXXX";
$database = "XXXXX";

关于php - FreeTDS mssql_connect 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725248/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com