PDO的使用指南:使用多個查詢來操作Sql Server
<p>我想執行一些不傳回結果集的查詢,然後執行一個<em>真實</em>的查詢,並取得其結果。
這是一個不起作用的範例:</p>
<pre class="brush:php;toolbar:false;"><?php
try {
$db = new PDO('dblib:host=myhost;dbname=master','user','password');
$query = "declare @entier int = 1;";
$db->exec($query);
$query = "select @entier;";
$stmt = $db->query($query);
$rows = $stmt->fetchAll();
print_r($rows);
}
catch (PDOException $e) {
print ($e->getMessage());
}
catch (Exception $e) {
print ($e->getMessage());
}
?></pre>
<p>這段程式碼也不行:
</p>
<pre class="brush:php;toolbar:false;">try {
$db = new PDO('dblib:host=myhost;dbname=master','user','password');
$query = "declare @entier int = 1; select @entier;";
$stmt = $db->query($query);
$rows = $stmt->fetchAll();
print_r($rows);
}
catch (PDOException $e) {
print ($e->getMessage());
}
catch (Exception $e) {
print ($e->getMessage());
}
?></pre>
<p>但是這段程式碼可以運行:</p>
<pre class="brush:php;toolbar:false;"><?php
try {
$db = new PDO('dblib:host=myhost;dbname=master','user','password');
$query = "select 1;";
$stmt = $db->query($query);
$rows = $stmt->fetchAll();
print_r($rows);
}
catch (PDOException $e) {
print ($e->getMessage());
}
catch (Exception $e) {
print ($e->getMessage());
}
?></pre>
<p>謝謝您的幫忙</p>