2

试图通过 mysqli 在 php 中创建存储过程。当我通过 php 执行此操作时,继续返回 1064 错误,但是当我在 php.ini 中复制相同的 sql 命令时,这很好。任何人都能够阐明可能是什么问题,或者任何人找到了类似问题的解决方案?

创建过程代码:

DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
    BEGIN
        DECLARE done INT DEFAULT FALSE;
        DECLARE a INT;
        DECLARE cur1 CURSOR FOR SELECT t.`task_id`
            FROM `task_dependency` d
            LEFT OUTER JOIN (
                SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
                FROM `task_dependency`
                LEFT OUTER JOIN (
                    SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
                ) ti ON ti.`task_id` = `dependent_on_task_id`
                GROUP BY `task_id`
            ) t ON d.`task_id` = t.`task_id`
            WHERE d.`dependent_on_task_id` = taskID
                AND t.make_live > 0;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

        OPEN cur1;

        read_loop: LOOP
            FETCH cur1 INTO a;
            IF done THEN
                LEAVE read_loop;
            END IF;
            UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
        END LOOP;

        CLOSE cur1;

        UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
            SELECT `task_id`
            FROM `task_dependency`
            WHERE `dependent_on_task_id` = taskID
        ) AND `earliest_start` < avaliableTime;
    END //
DELIMITER ;

PHP代码:

if (!$this->dbc->query("DROP PROCEDURE IF EXISTS " . $this->table_prefix . "makeLive") ||
    !$this->dbc->query($query)) {
    echo "Stored procedure creation failed: (" . $this->dbc->errno . ") " . $this->dbc->error . "<br /><br />" . $query;
    exit;
}
echo "success";
exit;

脚本执行抛出以下错误...

Stored procedure creation failed: (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER //
        CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN tas' at line 1

虽然它说这是一个语法错误,但直接将命令复制并粘贴到 phpmyadmin 的事实似乎可以工作,看起来它可能是某种 mysqli 错误?

4

3 回答 3

0

查询功能只允许您执行单个查询。尝试使用 multi_query 函数(文档:http ://www.php.net/manual/en/mysqli.multi-query.php )。它允许多个命令,并且可以解决您的问题。

于 2013-08-07T00:27:08.493 回答
0

结果 mysqli->query 已经解析了分号并运行了整个命令。查询开头和结尾处的分隔符命令在成功创建过程中是不必要的。

于 2013-08-07T00:47:49.257 回答
0

只需删除 DELIMITER 即可。所以在你的情况下,它会是这样的:

CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE a INT;
    DECLARE cur1 CURSOR FOR SELECT t.`task_id`
        FROM `task_dependency` d
        LEFT OUTER JOIN (
            SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
            FROM `task_dependency`
            LEFT OUTER JOIN (
                SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
            ) ti ON ti.`task_id` = `dependent_on_task_id`
            GROUP BY `task_id`
        ) t ON d.`task_id` = t.`task_id`
        WHERE d.`dependent_on_task_id` = taskID
            AND t.make_live > 0;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur1;

    read_loop: LOOP
        FETCH cur1 INTO a;
        IF done THEN
            LEAVE read_loop;
        END IF;
        UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
    END LOOP;

    CLOSE cur1;

    UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
        SELECT `task_id`
        FROM `task_dependency`
        WHERE `dependent_on_task_id` = taskID
    ) AND `earliest_start` < avaliableTime;
END;
于 2018-08-30T06:57:57.127 回答