无法用mysqli创建程序,但可以用phpmyadmin吗?

时间:2013-08-07 00:14:44

标签: php mysql mysqli

尝试通过php中的mysqli创建存储过程。当我通过php执行此操作时,继续抛出1064错误,但是当我在php中复制相同的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错误?

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

结果显示mysqli-&gt;查询已解析分号并运行整个命令。在查询开始和结束时的分隔符命令,在成功创建过程时没有必要。

答案 2 :(得分: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;