Created
June 5, 2024 18:41
-
-
Save theking2/62313d972f73c1b76bbe19bd15edb670 to your computer and use it in GitHub Desktop.
call sp and return the last paramete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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