Skip to content

Instantly share code, notes, and snippets.

@tonholis
Last active September 6, 2017 15:33
Show Gist options
  • Save tonholis/075ede738fc7bb27d2a3377c2b7a8e35 to your computer and use it in GitHub Desktop.
Save tonholis/075ede738fc7bb27d2a3377c2b7a8e35 to your computer and use it in GitHub Desktop.
Laravel and Store Procedures with Output parameters
/*
This example shows how to execute a stored procedured in MySQL in Laravel 5.3 and get the output values.
*/
//Input params
$search = 'test';
$page = 1;
$perPage = 10;
//Output values
$pageCount = 0;
$recordCount = 0;
$pdo = DB::connection()->getPdo();
$stmt = $pdo->prepare('CALL my_paginated_search_proc(:search,:pageNum,:pageSize,:pageCount,:recordCount);');
$stmt->bindParam(':search', $search);
$stmt->bindParam(':pageNum', $page);
$stmt->bindParam(':pageSize', $perPage);
$stmt->bindParam(':pageCount', $pageCount, \PDO::PARAM_INT|\PDO::PARAM_INPUT_OUTPUT);
$stmt->bindParam(':recordCount', $recordCount, \PDO::PARAM_INT|\PDO::PARAM_INPUT_OUTPUT);
$stmt->execute();
//Read the result
$results = $stmt->fetchAll();
$stmt->nextRowSet();
$stmt->closeCursor();
//Read output params
$output = DB::select("SELECT @pageCount, @recordCount");
dd($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment