我正在尝试连接到我的php脚本上的Azure Microsoft SQL Server数据库。我无法弄清楚它为什么不起作用。当我运行db_connection.php脚本时,我收到此错误:
SQLSTATE [01002] Adaptive Server连接失败(严重级9)
当我运行tsql命令时,使用我的azure ms sql数据库的连接详细信息,连接似乎有效(我读了" 1>"表示连接有效):
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "UTF-8"
Default database being set to iBalekaDB
1>
在我的freetds.conf文件中,我已经设置了这个配置:
# server specific section
[global]
# TDS protocol version
tds version = 8.0
text size = 20971520
client charset = UTF-8
dump file = /tmp/freetds.log
debug flags = 0xffff
# Command and connection timeouts
; timeout = 10
[iBalekaServer]
host = xxxxxxxx.xxxxxxx.windows.net
port = 1433
tds version = 8.0
client charset = UTF-8
我的db_connection.php文件如下所示:
try {
$dataSource = "dblib:host=iBalekaServer;dbname=iBalekaDB;";
$username = "xxxxxxxxxxxx";
$password = "xxxxxxxxxxxx";
$connectionObject = new PDO($dataSource, $username, $password);
$connectionObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($connectionObject) {
echo "<h2>Connection Successful</h2>";
} else {
echo "Connection Error";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
我在VPS上运行tsql -C并得到了这个:
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
我检查了是否安装了pdo_dblib,当我在Linux VPS服务器上运行phpinfo()时它就出现了。
这可能是什么问题?
编辑:使用mssql_connect有效。我真的很想使用PDO
答案 0 :(得分:1)
在我的测试中,我将n
更改为log(n)
的格式(例如$username
),并修复了UID
的问题。
顺便说一下,您可以从Azure门户网站的连接字符串中获取<username>@<db_server_name>
。
此外,如果您遇到SQLSTATE[01002] Adaptive Server connection failed (severity 9)
问题,可以参考PDO DBLib not working的答案。
任何更新,请随时告诉我。