Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP + MySql + Stored Procedures, how do I get access an "out" value?

Documentation is severely lacking on anything to do with stored procedures in mysql with PHP. I currently have a stored procedure that I call via PHP, how can I get the value of an out parameter?

like image 330
mmattax Avatar asked Sep 07 '08 04:09

mmattax


People also ask

Can MySQL stored procedure return value?

MySQL stored function returns only one value. To develop stored programs that return multiple values, you need to use stored procedures with INOUT or OUT parameters.

Can procedure return a value?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.

How do I query a stored procedure in MySQL?

Create a simple stored procedure. DELIMITER ; To create the MySQL Stored Procedure, open the MySQL workbench Connect to the MySQL Database copy-paste the code in the query editor window click on Execute. You can view the procedure under stored procedures.


1 Answers

it looks like it's answered in this post:

http://forums.mysql.com/read.php?52,198596,198717#msg-198717

With mysqli PHP API:

Assume sproc myproc( IN i int, OUT j int ):

$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, "&nbsp;";
      }
      $result->close();
      if( $mysqli->more_results() ) echo "<br/>";
    }
  } while( $mysqli->next_result() );
}
$mysqli->close();
like image 149
John Boker Avatar answered Sep 29 '22 13:09

John Boker