2013-02-04 25 views
0

我試圖讓PDO啓動並運行在我的Apache服務器上,因此我可以在與MS SQL數據庫交互時使用預準備語句。看來,庫安裝,並且連接好了,但我不能得到它的任何信息,從表中返回MS SQL PDO將不會從表中檢索數據

$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

請檢查'$ stmt-的返回值>的execute();'確認查詢是成功的。 –

+0

@PhilipWhitehouse謝謝。你的意思是這樣的:'print_r($ stmt-> execute());'?那沒有產生任何東西。 – 1252748

+0

@PhilipWhitehouse其實我有一個錯字。 ''stmt-> execute();''產生'Array()'' – 1252748

回答

1

(從評論排查...)

嘗試檢查的$stmt->execute();的返回值來確認查詢是成功的。

$stmt->execute();返回值是真實的成功,所以我會做:

$success = $stmt->execute(); 

if(!$success) { 
    print_r($stmt->errorInfo()); 
    die("DB Error"); 
}