Getting Access to OUT Parameters in PHP with MySQL Stored Procedures
When working with stored procedures in MySQL using PHP, obtaining the value of an "OUT" parameter can be a challenge due to limited documentation. However, this process is possible by utilizing the mysqli PHP API.
Using mysqli
Consider a stored procedure named "myproc" with one IN parameter ("i") and one OUT parameter ("j"). To retrieve the value of "j" using PHP, follow these steps:
<code class="php">$mysqli = new mysqli( "HOST", "USR", "PWD", "DBNAME" ); $ivalue=1; // Execute the stored procedure and store the result. $res = $mysqli->multi_query( "CALL myproc($ivalue,@x);SELECT @x" ); if( $res ) { $results = 0; // Iterate through the results. do { if ($result = $mysqli->store_result()) { // Display the result header. printf( "<b>Result #%u</b>:<br/>", ++$results ); // Fetch and display the OUT parameter value. while( $row = $result->fetch_row() ) { foreach( $row as $cell ) { echo $cell, "&nbsp;"; } } $result->close(); if( $mysqli->more_results() ) echo "<br/>"; } } while( $mysqli->next_result() ); } // Close the mysqli connection. $mysqli->close();</code>
In this example, the "j" parameter's value is stored in the "result" object and fetched using the fetch_row() method. This allows us to access and display the value of the OUT parameter from the stored procedure.
The above is the detailed content of How to Access OUT Parameters in PHP When Working with MySQL Stored Procedures?. For more information, please follow other related articles on the PHP Chinese website!