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>