【问题标题】:Not able to access multiple result rowsets from stored procedure in CakePHP 3.4无法从 CakePHP 3.4 中的存储过程访问多个结果行集
【发布时间】:2018-08-24 10:02:02
【问题描述】:

我正在尝试访问从我的 CakePHP 3.4 项目中的存储过程返回的多个结果集。这是我的代码:

$stmt = $db->execute("call mydatasp($paramlist)");
    $result = array();                     
    try{
            do
            {
                $rowset = $stmt->fetchAll('assoc');
                $result[]=$rowset;
            } while($stmt->nextRowset());
        }
        catch(Exception $e){}

我做了很多搜索,也浏览了这个链接,但没有找到任何有用的数据。 How to call PDOStatement::nextRowset() in Cakephp 3

我该怎么做?

【问题讨论】:

  • 那么在您链接的问题的答案中使用代码有什么问题?
  • @ndm 感谢您的快速回复。我已经尝试了代码,但它只给了我第一个结果集而不是全部。这是我的代码。在 $result 数组中,我只得到第一个结果集 - try{ do { $rowset = $stmt->fetchAll('assoc'); $结果[]=$行集; } while($stmt instanceof StatementDecorator); }
  • 我明白了,你不会这样使用它。您可以使用它从从 execute() 调用收到的语句装饰器 ($stmt) 中提取最里面的 PDO 语句,然后您可以使用原始的 do...while 循环对其进行迭代。
  • 我已经更新了my answer 并提供了一个更清晰的示例。
  • 这表明你没有使用我回答中的代码。

标签: php mysql cakephp pdo cakephp-3.0


【解决方案1】:
$connection = ConnectionManager::get('default');

$sql = "CALL test()"; // name of stored procedure

$rowSet = 1; //default value is 1,change according to stored procedure result set to access multiple result

$stmt = $connection->execute($sql)->getInnerStatement()->getInnerStatement();

for ($i = 0; $i < $rowSet; $i++) {
    if($i > 0)
        $stmt->nextRowset();
    $response[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

print_r($response);

假设存储过程test()返回4个结果集那么你需要修改$rowSet = 4的值,否则默认值为1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多