dtvgo28624 2013-11-18 20:33
浏览 65
已采纳

尝试连接/查询Microsoft Azure SQL数据库时,DBLIB PDO驱动程序一直出现故障

So I'm using Linux (ubuntu) and I'm trying to connect to a Microsoft Azure SQL database.

Server information:

  • The server address is a1a1a1a1a1.database.windows.net (a1a1a1a1a1 is a placeholder for my server name)

  • The database name is MyDatabase

  • The table is called [dbo].[Sloth]

Freetds.conf settings:

[global]
    dump file = /tmp/freetds.log
    debug flags = 0xffff
    text size = 64512

[a1a1a1a1a1.database.windows.net]
    host = a1a1a1a1a1.database.windows.net
    port = 1433
    tds version = 8.0
    client charset = UTF-8

When I run

php -r "phpinfo();" | grep "PDO drivers"

in terminal it returns

PDO drivers => dblib, mysql

so to the best of my knowledge all the configuration and driver installations are how they should be.

So now for the errors:

ERROR 1

If I initialize the PDO like so:

$conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);

it connects, but if I remove the spaces around the '=' character for dbname like so:

$conn = new \PDO ( "dblib:dbname=MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);

it returns these errors from the PDOException and freetds.log

SQLSTATE[HY000] General SQL Server error: Check messages from the SQL Server (severity 16)

(dbutil.c:86):msgno 40508: "USE statement is not supported to switch between databases. Use a new connection to connect to a different Database."

ERROR 2

When I run the following code

try {
    $conn = new \PDO ( "dblib:dbname = MyDatabase;host=a1a1a1a1a1.database.windows.net;", $Username, $Password);
    $conn->setAttribute( \PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION );
    $statement = $conn->prepare("SELECT * FROM dbo.Sloth");
    $result = $statement->execute();
}
catch ( PDOException $e ) {
    print( "Error connecting to SQL Server." );
    die(print_r($e));
}

it breaks at the $result = ... line with these errors from the PDOException and freetds.log

SQLSTATE[HY000]: General error: 208 General SQL Server error: Check messages from the SQL Server [208] (severity 16) [(null)] 

(dbutil.c:86):msgno 208: "Invalid object name 'dbo.Sloth'."

but when I run the query

SELECT * FROM dbo.Sloth

in the Azure Database Management Portal it returns all the correct rows.

Any help with these issues will be much appreciated!

EDIT

Through some tests I've discovered that both problems stem from the fact that the PDO isn't connecting to the database but is instead connecting to the "master". This would explain ERROR 2's Invalid Object Name error.

Any thoughts as to why the code in ERROR 1 doesn't connect to MyDatabase?

  • 写回答

1条回答 默认 最新

  • dongmu5106 2013-11-20 00:10
    关注

    SOLUTION

    I was using PHP 5.3.10 which had the following bug still in it, https://bugs.php.net/bug.php?id=64338

    Turns out the PDO driver was making a USE statement which Microsoft Azure SQL Database doesn't like.

    I upgraded to PHP 5.4.21 and everything worked properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题