note 129110 added to mysqli.execute-query
| From: | theking2 at main dot php dot net | Date: | Sat, 16 Dec 2023 19:24:15 +0000 |
| Subject: | note 129110 added to mysqli.execute-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221064@lists.php.net to get a copy of this message | ||
A function to call a stored procedure with an arbitrary number of IN parameters and one OUT
parameter, for instance returning affected row count. The return value of yhe function is this
value.
<?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( \mysqli $db, string $sp_name, ...$params ): int
{
$sql = "CALL $sp_name( ";
$sql .= implode( ", ", array_fill( 0, count( $params ), "?" ) );
$sql .= ", @__affected );";
$result = $db->execute_query( $sql, $params );
$result = $db->query( "select @__affected;" );
$affected = (int) $result->fetch_column( 0 );
return $affected;
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 185.163.92.217)
----
Manual Page -- https://php.net/manual/en/mysqli.execute-query.php
Edit -- https://main.php.net/note/edit/129110
Del: integrated -- https://main.php.net/note/delete/129110/integrated
Del: useless -- https://main.php.net/note/delete/129110/useless
Del: bad code -- https://main.php.net/note/delete/129110/bad+code
Del: spam -- https://main.php.net/note/delete/129110/spam
Del: non-english -- https://main.php.net/note/delete/129110/non-english
Del: in docs -- https://main.php.net/note/delete/129110/in+docs
Del: other reasons-- https://main.php.net/note/delete/129110
Reject -- https://main.php.net/note/reject/129110
Search -- https://main.php.net/manage/user-notes.php