Retrieving Multiple Result sets with stored procedure in php/mysqli

后端 未结 3 2018
暗喜
暗喜 2020-11-27 07:05

I have a stored procedure that has multiple result sets. How do I advance to the 2nd result set in mysqli to get those results?

Let\'s say it\'s a stored proc like:<

3条回答
  •  自闭症患者
    2020-11-27 07:39

    This has worked really well for me, it will deal with (as an example) as many Select Lists as there are in your SP. Note how you have to close the $call BEFORE you can then get to the OUT parameters from your SP...

    ?>
    num_rows > 0) {
                    $ctr = 0;
                    while ($row = $recordset->fetch_assoc()) {
                        ++$ctr;
                        //print_r($row);
                        echo "\t" . $ctr . ": ";
                        forEach($row as $key => $val) {
                            echo "[" . $key . "] " . $val . "\t";
                        }
                        echo "\n";
                    }
                }
                echo $recordset->num_rows . " record" . ($recordset->num_rows == 1 ? "" : "s") . ".\n";
                // Clean up, ready for next iteration...
                mysqli_free_result($recordset);
    
                // See if we can get another Recordset...
                mysqli_stmt_next_result($call);
            }
    
            // Then you have to close the $call...
            mysqli_stmt_close($call);
            // ...in order to get to the SP's OUT parameters...
            $select = mysqli_query($db, "SELECT @result");
            $row = mysqli_fetch_row($select);
            $result = $row[0];
            echo "\nOUT @result = " . $result . "\n";
        }
    }
    ?>

    And this is what the output from the above code looks like using my test_lists SP...

    Recordset #1...
        1: [s_1] 4  [user_name] Andrew Foster   
        2: [s_1] 4  [user_name] Cecil   
        3: [s_1] 4  [user_name] Sheff   
    3 records.
    
    Recordset #2...
        1: [s_2] Hello world!   [section_description] The Law   
        2: [s_2] Hello world!   [section_description] History   
        3: [s_2] Hello world!   [section_description] Wisdom Literature 
        4: [s_2] Hello world!   [section_description] The Prophets  
        5: [s_2] Hello world!   [section_description] The Life of Jesus and the Early Church    
        6: [s_2] Hello world!   [section_description] Letters from the Apostle Paul 
        7: [s_2] Hello world!   [section_description] Other Letters from Apostles and Prophets  
        8: [s_2] Hello world!   [section_description] Prophecy - warnings for the present and revelation of the future  
    8 records.
    
    OUT @result = 16
    

提交回复
热议问题