Home > Database > Mysql Tutorial > How to Access OUT Parameters in PHP When Working with MySQL Stored Procedures?

How to Access OUT Parameters in PHP When Working with MySQL Stored Procedures?

Mary-Kate Olsen
Release: 2024-10-30 01:44:03
Original
476 people have browsed it

How to Access OUT Parameters in PHP When Working with MySQL Stored Procedures?

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, "&amp;nbsp;";
                }
            }

            $result->close();
            if( $mysqli->more_results() ) echo "<br/>";
        }
    } while( $mysqli->next_result() );
}

// Close the mysqli connection.
$mysqli->close();</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template