0

我正在尝试为我的 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 在某种程度上正在发挥作用。我只是有一些不好的语法吗?

4

1 回答 1

1

(来自评论中的故障排除......)

尝试检查返回值$stmt->execute();以确认查询成功。

成功时的返回值为$stmt->execute();真,所以我会这样做:

$success = $stmt->execute(); 

if(!$success) {
   print_r($stmt->errorInfo()); 
   die("DB Error");
}
于 2013-02-04T23:41:53.417 回答