作为我的问题的延伸:Remote connect to SQL Server Standard Edition from PHP/FreeBSD
我正在尝试让我们的 FreeBSD/Apache/PHP 服务器能够查询我们的 WinServer2003/SQL-Server-2000 机器。请注意,这不是针对公共(public)系统,仅针对内部报告功能 - 因此此时性能不是关键。 最初的问题帮助我准备了 MSSQL 服务器以进行连接,现在从它那里得到了某种响应。但是我还没有成功连接到它。
我已经尝试过 PDO_dblib、mssql (FreeTDS) - 还没有尝试过 ODBC。如果我能让 PDO 工作,我会更愿意,所以这就是我的目标 - 它也是我尝试过的两个中最成功的。
我使用 PDO:dblib 的 PHP 脚本
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
$servername = '192.168.1.51';
$port = '1433';
$serverdsn = $servername.':'.$port;
$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';
$sqlstatement = 'SELECT * FROM ordersp';
try
{
$pdo = new PDO ("dblib:host=".$serverdsn.";dbname=".$dbname,$username,$password);
}
catch (PDOException $e)
{
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
exit;
此 PDO:dblib 脚本在浏览器中运行时的结果:
Failed to get DB handle: SQLSTATE[28000] Login incorrect. (severity 9)
在 MSSQL 服务器的应用程序日志中,我发现了这个:
在对 FreeTDS 发表评论后进行编辑
我的 PHP 脚本利用 mssql_connect()/FreeTDS
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';
$sqlstatement = 'SELECT * FROM ordersp';
$link = mssql_connect('MYMSDN', $username, $password);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
此 mssql/FreeTDS 脚本在浏览器中运行时的结果:
Warning: mssql_connect(): Unable to connect to server: MYMSDN in
/home/www/[..]/httpdocs/public/default/philip/oneserver-db-test.php
on line 17 Something went wrong while connecting to MSSQL
/usr/local/etc/freetds.conf
[global]
# TDS protocol version
tds version = 4.2
initial block size = 512
# uses some fixes required for some bugged MSSQL 7.0 server tha
# return invalid data to big endian clients
# NOTE TDS version 7.0 or 8.0 should be used instead
; swap broken dates = no
; swap broken money = no
# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
dump file = /tmp/freetds.log
; debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out of memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# (Microsoft servers sometimes pretend TEXT columns are
# 4 GB wide!) If you have this problem, try setting
# 'text size' to a more reasonable limit
text size = 64512
# A typical Microsoft SQL Server 2000 configuration
[MYMSDN]
host = 192.168.1.51
port = 1433
tds version = 8.0
client charset = UTF-8
文件 /tmp/freetds.log
在执行脚本时没有显示任何内容,但是如果我使用 # tsql -C
命令,它会更新。
最佳答案
下面几行代码行得通吗?
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
$username = 'webserver';
$password = '123456';
$dbname = 'oneServer_staging';
$servername = '192.168.1.51';
if (!$link = mssql_connect($servername, $username, $password)) {
exit('Error: Could not make a database connection using ' . $username . '@' . $servername);
}
if (!mssql_select_db($dbname, $link)) {
exit('Error: Could not connect to database ' . $dbname);
}
?>
关于php - FreeBSD 上 PHP 的 MSSQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14375441/