Skip to content

Instantly share code, notes, and snippets.

@theking2
Created June 5, 2024 18:41
Show Gist options
  • Save theking2/62313d972f73c1b76bbe19bd15edb670 to your computer and use it in GitHub Desktop.
Save theking2/62313d972f73c1b76bbe19bd15edb670 to your computer and use it in GitHub Desktop.
call sp and return the last paramete
function call_sp( \mysqli|\PDO $db, string $sp_name, ...$params ): string
{
$placeholders = array_fill( 0, count( $params ), "?" );
$placeholders[] = "@__newid;
$sql = "CALL $sp_name( " . implode( ", ", $placeholders ) . " );";
try {
LOG->debug( "calling Stored Procedure", [ "sql" => $sql ] );
if( $db instanceof \mysqli ) {
$db->execute_query( $sql, $params );
$result = $db->query( "select @__newid;" );
$newid = (string) $result->fetch_column( 0 );
} else {
$db->prepare( $sql )->execute( $params );
$newid= (string) $db->query( "select @__newid;" )->fetchColumn( 0 );
}
// get the number of affected rows
return $newid;
} catch ( \Exception $e ) {
LOG->error( "Error calling Stored Procedure", [ "sql" => $sql, "params" => $params, "error" => $e->getMessage() ] );
throw $e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment