简体   繁体   中英

PHP +MySQL + Stored Procedure, how to obtain result of single “out” value

I am aware that my question is very similar to this question . It is really more of a follow on/continued question.

$db = new mysqli($host, $user, $pass, $database);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
$db->query("SET @email  = " . "'" . $db->real_escape_string($recipient) . "'");
$db->query("SET @FormID   = " . "'" . $db->real_escape_string($list) . "'");
$db->query("SET @FieldName  = " . "'" . $db->real_escape_string($fieldName) . "'");
$db->query("SET @SubID  = " . "'" . $db->real_escape_string($action) . "'");
$db->query("SET @result = 2"); 

$rs = $db->multi_query("CALL EmailListCheck(@email, @FormID, @FieldName,@SubID, @result);SElect @result as result1");
if( $rs ) {
  $results = 0;
  do {
    if ($result = $db->store_result()) {
      printf( "<b>Result #%u</b>:<br/>", ++$results );
      while( $row = $result->fetch_row() ) {
        foreach( $row as $cell ) echo $cell, "&nbsp;";
      }
      $result->close();
      if( $db->more_results() ) echo "<br/>";
    }
  } while( $db->next_result() );
}

When I run the code above I receive the following result sets:

Result #1:
2 45 test_654@test.net 
Result #2:
0 

The Result #1 it returning an intermediate part of the stored procedure. A select statement within the SP itself that I will need to suppress.

I need the returned value from Results #2 to continue on with my script and determine how to proceed. How do I get the value from Result #2 into a variable that can then be used.

I also attempted to follow this example . When I attempt to run this section of code it returns “Fetch failed: (2014) Commands out of sync; you can't run this command now”

if(!$db->query("CALL EmailListCheck(@email, @FormID, @FieldName,@SubID, @result)"))
die("CALL failed: (" . $db->errno . ") " . $db->error);

// Fetch OUT parameters 
if (!($res = $db->query("SELECT @result AS result")))
    die("Fetch failed: (" . $db->errno . ") " . $db->error);
$row = $res->fetch_assoc();

// Return result
if($row['result'] == TRUE) {
    Echo 'WORKS';
}
else {
    return false;
} 

At the end of the day I need to return the single OUT value from the stored procedure and thus far have been unsuccessful.

Based on the suggestion in the comment from @Barmar I added this code: $db->next_result();

Full code is below. Not sure that this is best practice, but it works at this point.

    if(!$db->query("CALL EmailListCheck(@email, @FormID, @FieldName,@SubID, @result)"))
die("CALL failed: (" . $db->errno . ") " . $db->error);

$db->next_result();

// Fetch OUT parameters 
if (!($res = $db->query("SELECT @result AS result")))
    die("Fetch failed: (" . $db->errno . ") " . $db->error);
$row = $res->fetch_assoc();

// Return result
if($row['result'] == TRUE) {
    Echo 'WORKS';
}
else {
    return false;
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM