设置ATTR_ERRMODE ERRMODE_EXCEPTION时,PHP PDO存储过程失败

时间:2017-09-09 06:17:08

标签: php mysql stored-procedures pdo

我有PHP PDO的问题,

将ERRMODE设置为ERRMODE_EXCEPTION时,以下存储过程不起作用(始终返回空结果集)

这是PHP代码:

        $id = 12;
        $sql = "CALL get_section_path( ? )";

        $dsn = "mysql:host=$host;dbname=$dbname";

        $connection = new PDO($dsn, $user, $pass);

        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

        $statement = $connection->prepare($sql);

        $statement->bindParam( 1, $id, PDO::PARAM_INT );

        $statement->execute();

        $result_array = $statement->fetchAll(PDO::FETCH_ASSOC);

        $statement->closeCursor();

这是存储过程代码:

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_section_path`(IN `p_section_id` INT)
BEGIN
    DECLARE d_title_ar VARCHAR(255);
    DECLARE d_title_en VARCHAR(255);
    DECLARE d_child_type INT;
    DECLARE d_parent_id INT;

    DECLARE  cur_section CURSOR FOR SELECT `section_id`, `title_ar`, `title_en`, 1 AS `child_type`, `parent_id` FROM `sections` WHERE section_id = p_section_id;

    DROP TABLE IF EXISTS `sections_temp`;
    CREATE TABLE IF NOT EXISTS `sections_temp` (
      `section_id` int(11),
      `title_ar`    varchar(255),
      `title_en`    varchar(255),
      `child_type`  tinyint(1),
      `parent_id`   int(11)
    );
    loop_label:  LOOP
            IF  p_section_id > 0 THEN
                OPEN  cur_section;
                FETCH cur_section INTO p_section_id, d_title_ar, d_title_en, d_child_type, d_parent_id;
                CLOSE cur_section;
                INSERT INTO `sections_temp` (`section_id`, `title_ar`, `title_en`, `child_type`, `parent_id`) 
                                    VALUES ( p_section_id, d_title_ar, d_title_en, d_child_type, d_parent_id );
                SET p_section_id = d_parent_id;
                ITERATE loop_label;
            ELSE
                LEAVE   loop_label;
            END  IF;
    END LOOP;
    SELECT * FROM `sections_temp` WHERE 1;
    DROP TABLE `sections_temp`;
END

我试图设置MYSQL_ATTR_USE_BUFFERED_QUERY为true / false;但我得到了同样的结果。

当我删除ERRMODE_EXCEPTION时,该过程正常。

0 个答案:

没有答案