how to fetch multiple result set from a mysql stored procedure in laravel

前端 未结 2 425
梦毁少年i
梦毁少年i 2020-12-18 13:21

I want to fetch multiple result sets from a stored procedure in laravel. Is there a way I can do this? Currently, I can get a single row\'s data using the below code:

<
2条回答
  •  一个人的身影
    2020-12-18 13:27

    I am using the following code and it works flawlessly. Change it to suit your needs.

    public static function CallRaw($procName, $parameters = null, $isExecute = false)
    {
        $syntax = '';
        for ($i = 0; $i < count($parameters); $i++) {
            $syntax .= (!empty($syntax) ? ',' : '') . '?';
        }
        $syntax = 'CALL ' . $procName . '(' . $syntax . ');';
    
        $pdo = DB::connection()->getPdo();
        $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
        $stmt = $pdo->prepare($syntax,[\PDO::ATTR_CURSOR=>\PDO::CURSOR_SCROLL]);
        for ($i = 0; $i < count($parameters); $i++) {
            $stmt->bindValue((1 + $i), $parameters[$i]);
        }
        $exec = $stmt->execute();
        if (!$exec) return $pdo->errorInfo();
        if ($isExecute) return $exec;
    
        $results = [];
        do {
            try {
                $results[] = $stmt->fetchAll(\PDO::FETCH_OBJ);
            } catch (\Exception $ex) {
    
            }
        } while ($stmt->nextRowset());
    
    
        if (1 === count($results)) return $results[0];
        return $results;
    }
    

    Example call:

    $params = ['2014-01-01','2014-12-31',100];
    $results = APIDB::CallRaw('spGetData',$params);
    

    The resulting call will be:

    CALL spGetData(?,?,?)
    

    If there is only one resultset, it will be returned as is. If there are more, it will return an array of result sets. The key is using $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);. Without it, a horrible SQLSTATE[HY000]: General error: 2053 exception will be thrown.

    The try{} catch() block is used to eliminate the resultsets that cannot be fetched. Particularly, I have procedures that returns two resultsets, one as a result of an update (or other execute statements) and the last one as the real data. The exception thrown on fetchAll() with an execute query will be PDOException.

    Warning: the function is not optimised. You can rewrite it with one single pass through the parameters.

提交回复
热议问题