在 MariaDB 上尝试示例 #5 时,会得到 1414。使用此函数调用存储过程,最后一个参数为 INOUT,返回像 (uu)id 或计数这样的值;
<?php
function call_sp( \PDO $db, string $sp_name, ...$params ): mixed
{
$placeholders = array_fill( 0, count( $params ), "?" );
$placeholders[] = "@new_id";
$sql = "CALL $sp_name( " . implode( ", ", $placeholders ) . " ); SELECT @new_id AS `new_id`";
try {
LOG->debug( "调用存储过程", [ "sql" => $sql ] );
$stmt = $db->prepare( $sql );
$i = 0;
foreach( $params as $param ) {
$stmt->bindValue( ++$i, $param );
}
$stmt->execute();
$new_id = $stmt->fetch( PDO::FETCH_ASSOC )['new_id'];
return $new_id;
} catch ( \Exception $e ) {
LOG->error( "调用存储过程出错", [ "sql" => $sql, "params" => $params, "error" => $e->getMessage() ] );
throw $e;
}