Zend 2:如何使用Zend \ Db \ Adapter执行多个SQL查询?

时间:2017-03-22 15:19:48

标签: zend-framework zend-framework2 zend-db

我执行此查询:

$query = "
    Select * From table1 Where id = 1;
    Select * From table2 Where name = 'test';
";

使用\Zend\Db\Adapter\Adapter

$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();

如何访问第二个结果? $rawResult->next()仅返回第一个查询的值。

1 个答案:

答案 0 :(得分:0)

使用两个不同的查询。

/*
 * First Query
 */
$query = "
    Select * From table1 Where id = 1
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();

/*
 * Second Query
 */
$query = "
    Select * From table2 Where name = 'test'
";
$stmt = $this->dbAdapter->query($query);
$rawResult = $stmt->execute();

我不相信它的工作方式与连续两个查询以这种方式发送的方式相同。

为避免代码重复,您可以将重复的代码包装到方法中。

$rawResult1 = $this->getResults("Select * From table1 Where id = 1");
$rawResult2 = $this->getResults("Select * From table2 Where name = 'test'");

function getResults(string $query)
{
    $stmt = $this->dbAdapter->query($query);
    return $stmt->execute();
}