duangang79177 2017-06-18 08:53
浏览 19

存储过程和mysqli看不到结果

I cant read result from a Stored procedure on PHP MySqli

My query returns like this

On php. It is called like this. but it always returns to zero num_rows

    $stmt = null;
    $stmt = $conn->prepare("CALL `insert_exam_schedule`(?,?,?,?,?,?,?,?);");
    $stmt->bind_param('sssissss',$_SESSION['ID'], $_REQUEST['subjectCode'], $_REQUEST['sectionCodeFull'], $_REQUEST['dayId'], $startTime, $endTime, $_REQUEST['room'], $_REQUEST['proctorId']);
    $stmt->execute();

    #TEMPS
    $iResult = $stmt->get_result();
    #$returnRow = $iResult->fetch_row();

    if ($iResult->num_rows > 0) {
        while ($iRow = $iResult->fetch_assoc()) {
            $resultArray[] = $iRow;
        }
        $json['sucess'] = true;
        $json['result'] = $resultArray;
        echo json_encode($json);
    }
    else{
        #resulted nothing
        $json['sucess'] = false;
        $json['result'] = "Failed: " . $iResult->num_rows . " : " . $stmt->affected_rows;
        echo json_encode($json);
    }

This is the SQL Stores Procedure.. It Inserts then Selects the one on select is what on my expectation to return but it goes to show it don't return or I am using the wrong way to fetch values.

CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_exam_schedule`(`uId` VARCHAR(32),
                               `iSubjectCode` VARCHAR(16),
                               `iSectionCodeFull` VARCHAR(3),
                               `iDayId` INT(10) UNSIGNED,
                               `iStart` TIME,
                               `iEnd` TIME,
                               `iRoom` VARCHAR(32),
                               `iProctorId` VARCHAR(32))

#VALIDATE SECTION SUBJECT COMBINATION
#check if start is less than end
#CHECK IF ROOM IS AVAILABLE AT TIME
#CHECK IF PROCTOR IS AVAILABLE AT TIME
#CHECK IF CLASS IS ALREADY SCHEDULED
#CHECK IF USER ENTERED IS A PROCTOR FOR REDUNDANCY

DECLARE time_a, time_b TIME DEFAULT NULL;
DECLARE done INT DEFAULT FALSE;
DECLARE room_cursor CURSOR FOR (SELECT `Start`, `End` FROM `exam_schedules` WHERE `Room` = `iRoom` AND `Day Id` = `iDayId`);
DECLARE proc_cursor CURSOR FOR (SELECT `Start`, `End` FROM `exam_schedules` WHERE `Proctor Id` = `iProctorId` AND `Day Id` = `iDayId`);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

#-------------------------------

SET @classId = (
    SELECT `classes`.`Id` FROM `classes`
    JOIN `subjects` ON `classes`.`Subject Id` = `subjects`.`Id`
    WHERE `Section Code Full` = `iSectionCodeFull` AND `subjects`.`Code` = `iSubjectCode`);

#VALIDATE SECTION SUBJECT COMBINATION
#must be 1
SET @classValid = (
    SELECT COUNT(`classes`.`Id`)
    FROM `classes`
    JOIN `subjects` ON `classes`.`Subject Id` = `subjects`.`Id`
    WHERE
    `subjects`.`Code` = `iSubjectCode` AND
    `classes`.`Section Code Full` = `iSectionCodeFull`);

#check if start is less than end
#must be >= 60
SET @timeDiff = (SELECT TIME_TO_SEC(TIMEDIFF(`iEnd`, `iStart`)) / 60);

#CHECK IF ROOM IS AVAILABLE AT TIME
#must be false
SET @roomHit = FALSE;
#set @roomTimeConflict = (SELECT COUNT(`Id`) FROM `exam_schedules`
#   WHERE `Room` = '206' AND (`Start` < '00:02:00' AND `End` > '01:00:00'));
OPEN room_cursor;
room_looper: LOOP
    FETCH room_cursor INTO time_a, time_b;

    IF done THEN 
        LEAVE room_looper;
    END IF;

    IF (time_a < `iEnd` AND time_b > `iStart`) THEN
        SET @roomHit = TRUE;
        LEAVE room_looper;
    END IF;
END LOOP;

#CHECK IF PROCTOR IS AVAILABLE AT TIME
#must be 0
SET @procHit = FALSE;

#set @proctorTimeConflict = (SELECT COUNT(`Id`) FROM `exam_schedules`
#   WHERE `Proctor Id` = '1010338' AND (`Start` < '01:00:00' AND `End` > '00:00:00'));
OPEN proc_cursor;
SET done = FALSE;
proc_looper: LOOP
    FETCH proc_cursor INTO time_a, time_b;

    IF done THEN
        LEAVE proc_looper;
    END IF;

    IF (time_a < `iEnd` AND time_b > `iStart`) THEN
        SET @procHit = TRUE;
        LEAVE proc_looper;
    END IF;
END LOOP;

#CHECK IF CLASS IS ALREADY SCHEDULED
#must be 0  
SET @classHit = (SELECT COUNT(`Class Id`) FROM `exam_schedules` WHERE `Class Id` = @classId);

#CHECK IF USER ENTERED IS A PROCTOR (x)
#must be 1
SET @validProf = (SELECT COUNT(`Id Number`) FROM `users` WHERE `Id Number` = `iProctorId` AND `Access Id` = 2);

#SELECT @classValid, @timeDiff, @roomHit, @procHit, @classHit, @validProf;
#-----------
IF (@classValid = 1 AND @timeDiff >= 60 AND @roomHit = FALSE AND @procHit = FALSE AND @classHit = 0 AND @validProf = 1) THEN
    INSERT INTO `db_main`.`exam_schedules`(
        `Class Id`,
        `Day Id`,
        `Start`,
        `End`,
        `Room`,
        `Proctor Id`
    )
    VALUES(
    @classId,
    `iDayId`,
    `iStart`,
    `iEnd`,
    `iRoom`,
    `iProctorId`
    );

    IF (ROW_COUNT() != 0) THEN
        SET @rank = 0;
        SET @subject = (SELECT `subjects`.`Code` FROM `classes` JOIN `subjects` ON `classes`.`Subject Id` = `subjects`.`Id` WHERE `Section Code Full` = `iSectionCodeFull`);
        SET @examDay = (SELECT `rank` FROM (SELECT @rank:=@rank+1 AS `rank`, `Id`, `Date` FROM `exam_dates` ORDER BY `Date` ASC) AS `datesRanked`
            WHERE `Id` LIKE `iDayId`);
        CALL `insert_logs`(`uId`,'1', CONCAT('Added schedule for ', @subject, ' ', `iSectionCodeFull`, ' on day ', @examDay, ' of examination.'));
    END IF;
END IF;

SELECT @classValid AS `classValid`, @timeDiff AS `timeDiff`, @roomHit AS `roomHit`, @procHit AS `procHit`, @classHit AS `classHit`, @validProf AS `validProf`;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题