我正在尝试为我的Apache服务器启动并运行PDO,这样我就可以在与MS SQL数据库交互时使用预准备语句。似乎安装了库并且连接好了,但我无法从表中返回任何信息
$dbh = new PDO ("dblib:host=$hostname;dbname=$database","$username","$password");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
$stmt = $dbh->prepare("SELECT * FROM asdf");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($result); //prints nothing
var_dump($stmt->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)); //prints nothing
返回
**array(0) { }**
然而
$dbh = new PDO("dblib:host=$hostname;dbname=$database", "$username", "$password");
} catch (PDOException $e) {
echo "Failed to get DB handle: ".$e - > getMessage()."\n";
}
$stmt = $dbh - > prepare("SELECT @@VERSION");
$stmt - > execute();
while ($row = $stmt - > fetch()) {
print_r($row);
}
unset($dbh);
unset($stmt);
打印出计算机详细信息。
Array ( [] => Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) Apr 22 2011 19:23:43 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor) [0] => Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) Apr 22 2011 19:23:43 Copyright (c) Microsoft Corporation Standard Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor) )
所以我认为PDO正在某种程度上发挥作用。我刚刚得到了一些错误的语法吗?
答案 0 :(得分:1)
(来自评论中的疑难解答......)
尝试检查$stmt->execute();
的返回值以确认查询是否成功。
$stmt->execute();
的返回值在成功时为真,所以我会这样做:
$success = $stmt->execute();
if(!$success) {
print_r($stmt->errorInfo());
die("DB Error");
}