简体   繁体   English

为什么我不能在PHP中创建MySQL存储过程?

[英]Why can't I create a MySQL stored procedure in PHP?

I tried to create a stored prucedure with the following code: 我尝试使用以下代码创建存储的过程:

DELIMITER //
CREATE PROCEDURE AddTrickBaseInvert(
    IN invertName       VARCHAR(16),
    IN invertType       TINYINT(1)
)
BEGIN
    INSERT INTO trick_bases (type)
    VALUES (1);
    INSERT INTO trick_bases_invert (
        baseId,
        name,
        type
    )
    VALUES (
        LAST_INSERT_ID(),
        name,
        type
    );
END //
DELIMITER ;

It worked when I tried to run a MySQL test on localhost/phpmyadmin/, but when I tried to create the same procedure using the identically same SQL code via PHP, it gave me the following error: 当我尝试在localhost / phpmyadmin /上运行MySQL测试时,它起作用了,但是当我尝试通过PHP使用相同的SQL代码创建相同的过程时,它给了我以下错误:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 致命错误:未被捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。 check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $$ CREATE PROCEDURE AddTrickBaseInvert( IN invertName ' at line 1 ... 检查与您的MariaDB服务器版本相对应的手册以获取正确的语法,以在第1行的'DELIMITER $$ CREATE PROCEDURE AddTrickBaseInvert(IN invertName')附近使用...

The PHP code is here: PHP代码在这里:

$sql = '
DELIMITER //
CREATE PROCEDURE AddTrickBaseInvert(
    IN invertName       VARCHAR(16),
    IN invertType       TINYINT(1)
)
BEGIN
    INSERT INTO trick_bases (type)
    VALUES (1);
    INSERT INTO trick_bases_invert (
        baseId,
        name,
        type
    )
    VALUES (
        LAST_INSERT_ID(),
        name,
        type
    );
END //
DELIMITER ;';
$oDb->exec($sql);

As you see, the SQL code is exactly the same, but somehow the procedure does not get created. 如您所见,SQL代码是完全相同的,但是以某种方式未创建该过程。 I tried to do researches but I could not find any answers yet. 我试图进行研究,但找不到任何答案。 Do I miss something essential from the codes above which is somehow included when I run the code in the SQL tab on localhost/phpmyadmin? 当我在localhost / phpmyadmin的SQL选项卡中运行代码时,是否错过了上面代码中包含的某些基本内容?

Thanks in advance :) 提前致谢 :)

  • You don't need to redefine Delimiter, when trying to Create a stored procedure using application code. 尝试使用应用程序代码创建存储过程时,无需重新定义定界符。
  • Also, to avoid exceptions in certain cases, call DROP PROCEDURE IF EXISTS before the Create procedure statement. 另外,为避免某些情况下的异常,请在创建过程语句之前调用DROP PROCEDURE IF EXISTS

Do the following instead: 而是执行以下操作:

// drop if procedure exists already or not
$sql_drop = 'DROP PROCEDURE IF EXISTS AddTrickBaseInvert';
$oDb->exec($sql_drop);

// Now call create procedure statement
$sql = '
CREATE PROCEDURE AddTrickBaseInvert(
    IN invertName       VARCHAR(16),
    IN invertType       TINYINT(1)
)
BEGIN
    INSERT INTO trick_bases (type)
    VALUES (1);
    INSERT INTO trick_bases_invert (
        baseId,
        name,
        type
    )
    VALUES (
        LAST_INSERT_ID(),
        name,
        type
    );
END';
$oDb->exec($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM