note 129287 added to pdo.prepared-statements

From: Date: Tue, 27 Feb 2024 11:30:01 +0000
Subject: note 129287 added to pdo.prepared-statements
Groups: php.notes 
Request: Send a blank email to php-notes+get-221214@lists.php.net to get a copy of this message
Example #5 gives an 1414 wenn tried on MariaDB. Use this function to call a stored procedure with the last parameter as INOUT returning a value like a (uu)id or a count; <?php /** * call_sp Call the specified stored procedure with the given parameters. * The first parameter is the name of the stored procedure. * The remaining parameters are the (in) parameters to the stored procedure. * the last (out) parameter should be an int like state or number of affected rows. * * @param mixed $sp_name The name of the stored procedure to call. * @param mixed $params The parameters to pass to the stored procedure. * @return int The number of affected rows. */ 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( "calling Stored Procedure", [ "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( "Error calling Stored Procedure", [ "sql" => $sql, "params" => $params, "error" => $e->getMessage() ] ); throw $e; } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.131 (proxied: 46.140.241.51) ---- Manual Page -- https://php.net/manual/en/pdo.prepared-statements.php Edit -- https://main.php.net/note/edit/129287 Del: integrated -- https://main.php.net/note/delete/129287/integrated Del: useless -- https://main.php.net/note/delete/129287/useless Del: bad code -- https://main.php.net/note/delete/129287/bad+code Del: spam -- https://main.php.net/note/delete/129287/spam Del: non-english -- https://main.php.net/note/delete/129287/non-english Del: in docs -- https://main.php.net/note/delete/129287/in+docs Del: other reasons-- https://main.php.net/note/delete/129287 Reject -- https://main.php.net/note/reject/129287 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221214) next »