PHP无法从mysqli语句中读取第二个结果集

时间:2018-06-01 06:53:32

标签: php mysql stored-procedures mysqli

我正在尝试使用PHP从存储过程中读取两个结果集。第一个结果集很好,但虽然第二个结果似乎存在,但我无法强迫php处理它。相反,当尝试绑定时,它会因旧栗子而失败

  

“PHP警告:mysqli_stmt_bind_result():绑定变量的数量   与预备语句中的字段数不匹配“。

public function GetSession($sessionId) {
        $conn = mysqli_connect(DbConstants::$servername, DbConstants::$username, DbConstants::$password, DbConstants::$dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $call = mysqli_prepare($conn, 'CALL Sp_Session_GetById(?)');
        mysqli_stmt_bind_param($call, 'i', $sessionId);

        mysqli_stmt_execute($call);

        mysqli_stmt_bind_result(
            $call,
            $id,
            $startTime,
            $duration,
            $description,
            $instructorId,
            $aircraftId,
            $display,
            $bookable,
            $cancelled
        );

        mysqli_stmt_fetch($call);
        $session = new Session($id, $startTime, $duration, $description, $instructorId, $aircraftId, $display, $bookable, $cancelled, []);

        if(mysqli_stmt_more_results($call)) { /* This is true */
            mysqli_stmt_next_result($call);
            /* This is where it goes wrong */
            mysqli_stmt_bind_result(
                $call,
                $sessionTypeId,
                $spaces,
                $enabled
            );
        }
}

如果我在mysql workbench中执行存储过程,我会得到两个结果选项卡,如下所示:

# id, startTime, duration, description, instructor_id, aircraft_id, display, bookable, cancelled
'38', '2018-05-19 09:00:00', '180', NULL, '18', '2', '1', '1', '0'

# sessiontype_id, spaces, enabled
'1', '3', '1'
'2', '3', '1'

存储过程创建如下:

DELIMITER //

CREATE PROCEDURE Sp_Session_GetById (IN
    _id int
)
BEGIN
SELECT 
    s.id,
    s.startTime,
    s.duration,
    s.description,
    s.instructor_id,
    s.aircraft_id,
    s.display,
    s.bookable,
    s.cancelled
FROM session s
WHERE s.id = _id
AND s.cancelled = false
AND s.display = true;

SELECT sst.sessiontype_id, sst.spaces, sst.enabled
FROM session_sessiontype as sst
WHERE sst.session_id = _id;

END //

DELIMITER ;

1 个答案:

答案 0 :(得分:0)

我有多个结果集正在使用Barmar建议使用mysqli_multi_query(),我将在下面发布。但是,它不使用mysqli_stmt_next_result(),所以如果有人确实使用mysqli_stmt_next_result()提供了一个可行的解决方案,我会将它们标记为已接受的解决方案。

hero-image