Home  >  Article  >  Database  >  How to Access Out Parameters in MySQL Stored Procedures with PHP?

How to Access Out Parameters in MySQL Stored Procedures with PHP?

DDD
DDDOriginal
2024-11-01 12:43:02550browse

How to Access Out Parameters in MySQL Stored Procedures with PHP?

Accessing Out Parameters in PHP MySql Stored Procedures

When working with stored procedures in MySQL using PHP, accessing the values of out parameters can be a challenge. This article provides a solution based on MySQL's documentation and a forum thread.

Method:

  1. Use the mysqli::multi_query() function to execute the stored procedure and store the result in a variable ($res).
  2. Instantiate a variable to store the out parameter value ($x in this example).
  3. Call mysqli::store_result() to retrieve the result set.
  4. Use mysqli::fetch_row() to iterate over the rows and display the values, including the out parameter value (@x).

Code Example:

<code class="php">$mysqli = new mysqli("HOST", "USR", "PWD", "DBNAME");
$ivalue = 1;
$res = $mysqli->multi_query("CALL myproc($ivalue, @x);SELECT @x");

if ($res) {
  $results = 0;
  do {
    if ($result = $mysqli->store_result()) {
      printf("<b>Result #%u</b>:<br/>", ++$results);
      while ($row = $result->fetch_row()) {
        foreach ($row as $cell) {
          echo $cell, " ";
        }
      }
      $result->close();
      if ($mysqli->more_results()) {
        echo "<br/>";
      }
    }
  } while ($mysqli->next_result());
}

$mysqli->close();</code>

By using this method, you can effectively access the values of out parameters in stored procedures from your PHP code.

The above is the detailed content of How to Access Out Parameters in MySQL Stored Procedures with PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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